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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot 使用Redis

發布時間:2023/12/6 javascript 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot 使用Redis 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載自:http://www.cnblogs.com/ityouknow/p/5748830.html

Redis支持更豐富的數據結構,例如hashes, lists, sets等,同時支持數據持久化。除此之外,Redis還提供一些類數據庫的特性,比如事務,HA,主從庫。可以說Redis兼具了緩存系統和數據庫的一些特性,因此有著豐富的應用場景

如何使用

1、引入 spring-boot-starter-redis

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> #這是我自己加進去的,如果不加,后續很多功能無法使用 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>

2、添加配置文件

# Redis數據庫索引(默認為0) spring.redis.database=0 # Redis服務器地址 spring.redis.host=127.0.0.1 # Redis服務器連接端口 spring.redis.port=6379 # Redis服務器連接密碼(默認為空) spring.redis.password= # 連接池最大連接數(使用負值表示沒有限制) spring.redis.pool.max-active=8 # 連接池最大阻塞等待時間(使用負值表示沒有限制) spring.redis.pool.max-wait=-1 # 連接池中的最大空閑連接 spring.redis.pool.max-idle=8 # 連接池中的最小空閑連接 spring.redis.pool.min-idle=0 # 連接超時時間(毫秒) spring.redis.timeout=0

3、添加cache的配置類

@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport{@Beanpublic KeyGenerator keyGenerator() {return new KeyGenerator() {@Overridepublic Object generate(Object target, Method method, Object... 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();}};}@SuppressWarnings("rawtypes")@Beanpublic CacheManager cacheManager(RedisTemplate redisTemplate) {RedisCacheManager rcm = new RedisCacheManager(redisTemplate);//設置緩存過期時間//rcm.setDefaultExpiration(60);//秒return rcm;}@Beanpublic RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {StringRedisTemplate template = new StringRedisTemplate(factory);Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);template.setValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}}

3、好了,接下來就可以直接使用了

@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(Application.class) public class TestRedis {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@Autowiredprivate RedisTemplate redisTemplate;@Testpublic void test() throws Exception {stringRedisTemplate.opsForValue().set("aaa", "111");Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));}
##第二個test沒有跑通@Testpublic void testObj() throws Exception {User user=new User("aa@126.com", "aa", "aa123456", "aa","123");ValueOperations
<String, User> operations=redisTemplate.opsForValue();operations.set("com.neox", user);operations.set("com.neo.f", user,1,TimeUnit.SECONDS);Thread.sleep(1000);//redisTemplate.delete("com.neo.f");boolean exists=redisTemplate.hasKey("com.neo.f");if(exists){System.out.println("exists is true");}else{System.out.println("exists is false");}// Assert.assertEquals("aa", operations.get("com.neo.f").getUserName());} }

?

?

轉載于:https://www.cnblogs.com/shuiyelifang/p/8135991.html

總結

以上是生活随笔為你收集整理的Spring Boot 使用Redis的全部內容,希望文章能夠幫你解決所遇到的問題。

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