連接配接到Redis
Redis連接配接工廠會生成到Redis資料庫伺服器的連接配接。Spring Data Redis為四種Redis用戶端實作提供了連接配接工廠:
JedisConnectionFactory
JredisConnectionFactory
LettuceConnectionFactory
SrpConnectionFactory
如果使用其他連接配接工廠可以簡單的替換如:
所有的Redis連接配接工廠都具有setHostName()、setPort()和setPassword()方法。這樣,它們在配置方面實際上是相同的。
Spring Data Redis提供了兩個模闆:
1.RedisTemplate
2.StringRedisTemplate
RedisTemplate可以極大地簡化Redis資料通路,能夠讓我們持久化各種類型的key和value,并不局限于位元組數組。在認識到key和value通常是String類型之後,StringRedisTemplate擴充了RedisTemplate,隻關注String類型。假設我們已經有了RedisConnectionFactory,那麼可以按照如下的方式建構RedisTemplate:
RedisTemplate的很多功能是以子API的形式提供的:
每個子API都提供了使用資料條目的操作,基于value中所包含的是單個值還是一個值的集合它們會有所差别。
由于方法太多我簡單的使用set和get方法做示範:(具體請檢視官方文檔)
@RestController
public class RedisTemplateController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping("redis/get")
public String get(){
stringRedisTemplate.opsForValue().set("zzf","歐哈喲");
return stringRedisTemplate.opsForValue().get("zzf");
}
@RequestMapping("redis/get2")
public String get2(){
redisTemplate.opsForValue().set("zzf2","歐哈喲哈哈哈");
return (String)redisTemplate.opsForValue().get("zzf2");
}
}
OK~
使用list的話如:
使用set如:
有興趣的可以去看api文檔學習這些方法……不過我個人更喜歡Jedis直覺,簡單友善……
使用key和value的序列化器
當某個條目儲存到Redis key-value存儲的時候,key和value都會使用Redis的序列化器(serializer)進行序列化。Spring Data Redis提供了當某個條目儲存到Redis key-value存儲的時候,key和value都會使用Redis的序列化器(serializer)進行序列化。
Spring Data Redis提供了多個這樣的序列化器,包括:
1.GenericToStringSerializer:使用Spring轉換服務進行序列化;
2.JacksonJsonRedisSerializer:使用Jackson 1,将對象序列化為JSON;
3.Jackson2JsonRedisSerializer:使用Jackson 2,将對象序列化為JSON;
4.JdkSerializationRedisSerializer:使用Java序列化;
5.OxmSerializer:使用Spring O/X映射的編排器和解排器(marshaler和unmarshaler)實作序列化,用于XML序列化;
6.StringRedisSerializer:序列化String類型的key和value。
假設當使用RedisTemplate的時候,我們希望将Product類型的value序列化為JSON,而key是String類型。RedisTemplate的setKeySerializer()和setValueSerializer()方法就需要如下所示:
下面附上我的SpringBoot內建RedisTemplate的配置步驟:
1.導入jar包,我這裡使用的是maven
<!--spring-data-redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.配置application.properties
# Redis資料庫索引(預設為0)
spring.redis.database=0
# Redis伺服器位址
spring.redis.host=127.0.0.1
# Redis伺服器連接配接端口
spring.redis.port=6379
# Redis伺服器連接配接密碼(預設為空)
spring.redis.password=123456789
# 連接配接池最大連接配接數(使用負值表示沒有限制)
#spring.redis.pool.max-active=1000
spring.redis.jedis.pool.max-active=1000
# 連接配接池最大阻塞等待時間(使用負值表示沒有限制)
#spring.redis.jedis.pool.max-wait=-1
# 連接配接池中的最大空閑連接配接
spring.redis.jedis.pool.max-idle=10
# 連接配接池中的最小空閑連接配接
spring.redis.jedis.pool.min-idle=2
# 連接配接逾時時間(毫秒)
spring.redis.timeout=5000
3.使用
package com.zzf.finals.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisTemplateController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping("redis/get")
public String get(){
stringRedisTemplate.opsForValue().set("zzf","歐哈喲");
return stringRedisTemplate.opsForValue().get("zzf");
}
@RequestMapping("redis/get2")
public String get2(){
redisTemplate.opsForValue().set("zzf2","歐哈喲哈哈哈");
return (String)redisTemplate.opsForValue().get("zzf2");
}
}
參考《spring實戰》