← 文章归档32-Spring Boot Redis
约 13 分钟阅读阅读 — 次
Spring Redis
- 尝试使用Spring和Redis结合进行使用
1. Redis的对应Maven依赖
1 2 3 4 5 6
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.5.RELEASE</version> </dependency>
|
1.1. 在对应的配置文件中的Redis配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| redis: database: 10 host: localhost port: 6349 password: root timeout: 0 pool: max-active: 8 max-wait: -1 max-idle: 8 min-idle: 0 timeout: 0
|
2. Java结合Rredis
- 首先需要配置完成对应的redis文件包
2.1. 连接到redis服务
1 2 3 4 5 6 7 8 9 10 11
| import redis.clients.jedis.Jedis; public class RedisJava { public static void main(String[] args) { Jedis jedis = new Jedis("localhost"); System.out.println("连接成功"); System.out.println("服务正在运行: "+jedis.ping()); } }
|
2.2. String(字符串)实例
1 2 3 4 5 6 7 8 9 10 11 12 13
| import redis.clients.jedis.Jedis; public class RedisStringJava { public static void main(String[] args) { Jedis jedis = new Jedis("localhost"); System.out.println("连接成功"); jedis.set("runoobkey", "www.runoob.com"); System.out.println("redis 存储的字符串为: "+ jedis.get("runoobkey")); } }
|
2.3. List实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import java.util.List; import redis.clients.jedis.Jedis; public class RedisListJava { public static void main(String[] args) { Jedis jedis = new Jedis("localhost"); System.out.println("连接成功"); jedis.lpush("site-list", "Runoob"); jedis.lpush("site-list", "Google"); jedis.lpush("site-list", "Taobao"); List<String> list = jedis.lrange("site-list", 0 ,2); for(int i=0; i<list.size(); i++) { System.out.println("列表项为: "+list.get(i)); } } }
|
2.4. Java Keys实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import java.util.Iterator; import java.util.Set; import redis.clients.jedis.Jedis; public class RedisKeyJava { public static void main(String[] args) { Jedis jedis = new Jedis("localhost"); System.out.println("连接成功"); Set<String> keys = jedis.keys("*"); Iterator<String> it=keys.iterator() ; while(it.hasNext()){ String key = it.next(); System.out.println(key); } } }
|
3. Spring Boot 合并 redis
3.1. config文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Bean public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) { CacheManager cacheManager = new RedisCacheManager(redisTemplate); return cacheManager; }
@Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>(); redisTemplate.setConnectionFactory(factory); return redisTemplate; } }
|
3.2. 实现Redis的Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| @Service public class RedisService { @Resource private RedisTemplate<String,Object> redisTemplate;
private volatile static ValueOperations<String,Object> vo;
private void initRedis(){ if(vo == null) { synchronized (RedisService.class) { if(vo == null) { vo = redisTemplate.opsForValue(); } } } }
public void set(String key, Object value) { initRedis(); vo.set(key, value); }
public String get(String key) { initRedis(); return (String)vo.get(key); }
public void setTime(String key, Object value, long seconds){ initRedis(); Duration duration = Duration.between(LocalDateTime.now(), LocalDateTime.now().plusSeconds(seconds)); vo.set(key, value, duration); } }
|
3.3. 参考
- SpringBoot 整合 Redis 的简单案例
评论