日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

springboot redis配置

發布時間:2023/11/27 生活经验 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot redis配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、引入maven依賴

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

?

2、redis連接配置

spring:redis:host: 10.220.1.41port: 6379timeout: 10000password:jedis:pool:#最大連接數max-active: 8#最大阻塞等待時間(負數表示沒限制)max-wait: -1ms#最大空閑max-idle: 8#最小空閑min-idle: 0

?

3、redisTemplate配置,其實springboot2不配置也是可以直接使用的,但是我們可以指定一下key,value序列化的方式,如下

@Configuration
public class RedisConfiguration extends CachingConfigurerSupport {@Bean@Overridepublic KeyGenerator keyGenerator() {return (target, method, params) -> {StringBuilder sb = new StringBuilder();sb.append(target.getClass().getName());sb.append(method.getName());for (Object obj : params) {sb.append(obj.toString());}return sb.toString();};}@Beanpublic CacheManager cacheManager(RedisConnectionFactory connectionFactory) {return RedisCacheManager.builder(connectionFactory).build();}@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(redisConnectionFactory);redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());redisTemplate.afterPropertiesSet();return redisTemplate;}
}

4、KeyPrefix類定義,方便管理key的前綴與超時時間(防止key管理混亂,出現后面的key覆蓋前面的key的情況)

public class KeyPrefix {private String prefix;private Long timeout;//0或負數或空是表示永不過期public KeyPrefix(String prefix, Long timeout) {this.prefix = prefix;this.timeout = timeout;}

  //所有的key前綴統一在這了定義,方便管理
public static KeyPrefix LOGIN_USER_KP = new KeyPrefix("login_user_",10000L);public String getPrefix() {return prefix;}public void setPrefix(String prefix) {this.prefix = prefix;}public Long getTimeout() {return timeout;}public void setTimeout(Long timeout) {this.timeout = timeout;} }

?

5、寫一個通用的服務類,鍵值對的插入和查詢等

@Component
public class RedisService<HK, V> {private RedisTemplate<String, V> redisTemplate;private HashOperations<String, HK, V> hashOperations;private ListOperations<String, V> listOperations;private ZSetOperations<String, V> zSetOperations;private SetOperations<String, V> setOperations;private ValueOperations<String, V> valueOperations;@Autowiredpublic RedisService(RedisTemplate<String, V> redisTemplate) {this.redisTemplate = redisTemplate;this.hashOperations = redisTemplate.opsForHash();this.listOperations = redisTemplate.opsForList();this.zSetOperations = redisTemplate.opsForZSet();this.setOperations = redisTemplate.opsForSet();this.valueOperations = redisTemplate.opsForValue();}public void hashPut(String key, HK hashKey, V value) {hashOperations.put(key, hashKey, value);}public Map<HK, V> hashFindAll(String key) {return hashOperations.entries(key);}public V hashGet(String key, HK hashKey) {return hashOperations.get(key, hashKey);}public void hashRemove(String key, HK hashKey) {hashOperations.delete(key, hashKey);}public Long listPush(String key, V value) {return listOperations.rightPush(key, value);}public Long listUnshift(String key, V value) {return listOperations.leftPush(key, value);}public List<V> listFindAll(String key) {if (!redisTemplate.hasKey(key)) {return null;}return listOperations.range(key, 0, listOperations.size(key));}public V listLPop(String key) {return listOperations.leftPop(key);}public void setValue(String key, V value) {valueOperations.set(key, value);}public void setValue(KeyPrefix kp, String key, V value) {Long timeout = kp .getTimeout();if(timeout == null || timeout <= 0){this.setValue(kp.getPrefix() + key, value);}else{this.setValue(kp.getPrefix() + key, value, kp.getTimeout());}}public void setValue(String key, V value, long timeout) {valueOperations.set(key, value, timeout, TimeUnit.MILLISECONDS);}public V getValue(String key) {return valueOperations.get(key);}public void remove(String key) {redisTemplate.delete(key);}public boolean expire(String key, long timeout, TimeUnit timeUnit) {return redisTemplate.expire(key, timeout, timeUnit);}}

?

6、測試

@SpringBootTest
@RunWith(SpringRunner.class)
@Component
public class SpringRedisTest {@Autowiredprivate RedisService<String, Users> redisService;@Testpublic void set(){Users user = new Users();user.setUserId(1L);user.setUsername("tom");user.setPassword("123");redisService.setValue("user_tom", user);}}

?

轉載于:https://www.cnblogs.com/lilianggui/p/10129001.html

總結

以上是生活随笔為你收集整理的springboot redis配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。