spring-data-redis
文檔:?https://docs.spring.io/spring-data/redis/docs/2.0.1.RELEASE/reference/html/#new-in-2.0.0Redis
?文檔:?https://redis.io/documentationRedis
?中文文檔:?http://www.redis.cn/commands.html
本文學習一下如何通過 Java 操作 Redis。
Java 操作 Redis 的庫有兩個,Jedis 和 Lettuce,目前 SpringBoot 2.x 中已經將 Jedis 換成了 Lettuce。
Lettuce
?和?Jedis
?的都是連接Redis Server
的客戶端程序。Jedis
在實現上是直連redis server
,多線程環境下非線程安全,除非使用連接池,為每個Jedis實例增加物理連接。Lettuce
基于Netty的連接實例(StatefulRedisConnection),可以在多個線程間并發訪問,且線程安全,滿足多線程環境下的并發訪問,同時它是可伸縮的設計,一個連接實例不夠的情況也可以按需增加連接實例。
本文直接從 Lettuce 來學習。
基本使用
1:添加依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope> </dependency>
?
屬性配置
在?application.properties
?文件中配置如下內容
spring.redis.host=localhost #spring.redis.password=battcn # 連接超時時間(毫秒) spring.redis.timeout=10000 # Redis默認情況下有16個分片,這里配置具體使用的分片,默認是0 spring.redis.database=0 # 連接池最大連接數(使用負值表示沒有限制) 默認 8 spring.redis.lettuce.pool.max-active=8 # 連接池最大阻塞等待時間(使用負值表示沒有限制) 默認 -1 spring.redis.lettuce.pool.max-wait=-1 # 連接池中的最大空閑連接 默認 8 spring.redis.lettuce.pool.max-idle=8 # 連接池中的最小空閑連接 默認 0 spring.redis.lettuce.pool.min-idle=0
?
?
?
實體類
創建一個User
類
private static final long serialVersionUID = 8655851615465363473L; private Long id; private String username; private String password;public Users(Long id, String username, String password) {this.id = id;this.username = username;this.password = password; }public static long getSerialVersionUID() {return serialVersionUID; }public Long getId() {return id; }public void setId(Long id) {this.id = id; }public String getUsername() {return username; }public void setUsername(String username) {this.username = username; }public String getPassword() {return password; }public void setPassword(String password) {this.password = password; }@Override public String toString() {return "Users{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +'}'; }
?
?
?
新建RedisCacheAutoConfiguration配置類
默認情況下的模板只能支持RedisTemplate<String, String>
,也就是只能存入字符串,這在開發中是不友好的,所以自定義模板是很有必要的,當自定義了模板又想使用String
存儲這時候就可以使用StringRedisTemplate
的方式,它們并不沖突
@Configuration @AutoConfigureAfter(RedisAutoConfiguration.class) public class RedisCacheAutoConfiguration {@Beanpublic RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {RedisTemplate<String, Serializable> template = new RedisTemplate<>();template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());template.setConnectionFactory(redisConnectionFactory);return template;} }
?
?
?
?
新建一個SpringbootRedisDemoApplicationTests類測試
private static final Logger log = LoggerFactory.getLogger(SpringbootRedisDemoApplicationTests.class);@Autowired private StringRedisTemplate stringRedisTemplate;@Autowired private RedisTemplate<String, Serializable> redisCacheTemplate;@PostMapping public void get() {ExecutorService executorService = Executors.newFixedThreadPool(8888);IntStream.range(0, 8888).forEach(i ->executorService.execute(() -> stringRedisTemplate.opsForValue().increment("dd", 1)));stringRedisTemplate.opsForValue().set("flag", "好");final String a = stringRedisTemplate.opsForValue().get("flag");log.info("[字符緩存結果] - [{}]", a);String key = "opsForValue:user:1";redisCacheTemplate.opsForValue().set(key, new Users(99L, "小王", "18"));final Users user = (Users) redisCacheTemplate.opsForValue().get(key);System.out.println(user + "*****");log.info("[對象緩存結果] - [{}]", user); }
?
用postman發起請求
?
客戶端工具顯示效果
?
下列的就是Redis
其它類型所對應的操作方式
- opsForValue:?對應 String(字符串)
- opsForZSet:?對應 ZSet(有序集合)
- opsForHash:?對應 Hash(哈希)
- opsForList:?對應 List(列表)
- opsForSet:?對應 Set(集合)
- opsForGeo:?對應 GEO(地理位置)
?
?
?
?
?
?
?
?