Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API,Redis也是技术领域使用最为普遍的存储中间件,它是「Remote Dictionary Service」首字母缩写,也就是「远程字典服务」。html
Redis相比Memcached提供更多的数据类型支持和数据持久化操做。java
访问官网:https://hub.docker.com/r/library/redis/ 选择下载版本,本文选择最新Stable 4.0.11git
使用命令拉取镜像:github
docker pull redis:4.0.11redis
启动Redis命令以下:spring
docker run --name myredis -p 6379:6379 -d redis:4.0.11 redis-server --appendonly yesdocker
命令说明:数据库
启动成功以后使用命令:缓存
docker psspringboot
查看redis运行请求,以下图为运行成功:
链接Redis不错的GUI工具应该是Redis Desktop Manager了,不过如今只有Linux版能够免费下载,我上传了一个Windows版本在百度云,版本号为:0.9.5(发布于2018.08.24)也是比较新的,连接: https://pan.baidu.com/s/16npZtnGa3-p2PAafiPEAkA 密码: 9uqg,仍是免安装的,很好用。
Redis Desktop Manager客户端预览:
开发环境
在pom.xml添加以下依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
注意不要依赖“spring-boot-starter-redis”它是旧版本,新版已经迁移到“spring-boot-starter-data-redis”了。
在application.properties进行以下设置:
# Redis 配置 # Redis服务器地址 spring.redis.host=127.0.0.1 # Redis服务器链接密码(默认为空) spring.redis.password= # Redis服务器链接端口 spring.redis.port=6379 # Redis分片(默认为0)Redis默认有16个分片 spring.redis.database=0 # 链接池最大链接数(使用负值表示没有限制) 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=10000 # 指定spring的缓存为redis spring.cache.type=redis
注意:spring.redis.timeout不要设置为0,设置为0查询Redis时会报错,由于查询链接时间过短了。
完成以上配置以后就能够写代码操做Redis了,示例代码以下:
@Autowired private StringRedisTemplate stringRedisTemplate; @RequestMapping("/") public String doTest() { String _key = "time"; //缓存key stringRedisTemplate.opsForValue().set(_key, String.valueOf(new Date().getTime())); //redis存值 return stringRedisTemplate.opsForValue().get(_key); //redis取值 }
更多操做:
为了简化缓存能够直接使用声名式缓存,能够省去设置缓存和读取缓存的代码,使用起来会方便不少。
声明式缓存使用步骤以下:
在pom.xml文件设置缓存为Redis,代码以下:
spring.cache.type=redis
在启动文件Application.java设置开启缓存,代码以下:
@SpringBootApplication @EnableCaching public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
注解以下:
通用属性:
示例代码以下:
// 缓存key private final String _CacheKey = "userCacheKeyTime"; @RequestMapping("/") @Cacheable(value = _CacheKey) public String index() { System.out.println("set cache"); return "cache:" + new Date().getTime(); }
只有首次访问的时候会在控制台打印“set cache”信息,以后直接返回Redis结果了,不会在有添加的打印信息出现。
示例代码以下:
// 缓存key private final String _CacheKey = "userCacheKeyTime"; @RequestMapping("/put") @CachePut(value = _CacheKey) public String putCache() { System.out.println("update cache"); return "update cache:" + new Date().getTime(); }
访问http://xxx/put 每次会把最新的数据存储缓存起来。
示例代码以下:
// 缓存key private final String _CacheKey = "userCacheKeyTime"; @RequestMapping("/del") @CacheEvict(value = _CacheKey) public String delCache() { System.out.println("缓存删除"); return "delete cache:" + new Date().getTime(); }
访问http://xxx/del 只会删除缓存,除此以后不会进行任何操做。
在分布式系统中Session共享有不少种方案,而把Session托管在缓存中是最经常使用的方案之一,下面来看Session在Redis中的托管步骤。
在pom.xml中添加以下引用:
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency>
在启动类Application.java的类注解添加开启Session,代码以下:
@SpringBootApplication @EnableCaching @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800) public class RedisApplication { public static void main(String[] args) { SpringApplication.run(RedisApplication.class, args); } }
其中maxInactiveIntervalInSeconds为Session过时时间,默认30分钟,设置单位为秒。
接下来编写一段代码来测试一下Session,示例代码以下:
@RequestMapping("/uid") public String testSession(HttpSession session) { UUID uid = (UUID) session.getAttribute("uid"); if (uid == null) { uid = UUID.randomUUID(); } session.setAttribute("uid", uid); return session.getId(); }
连续访问两次请求以后,查看控制台信息以下图:
能够看出,两次访问的SessionId是同样的,这个时候在查看Redis 客户端,以下图:
发现Redis里存储的Session过时时间也是对的,符合咱们的设置。
由于把Session托管给同一台Redis服务器了,因此Session在Spring Boot中按照如上方式在配置多台服务器,获得的Session是同样的。
示例源码下载:https://github.com/vipstone/springboot-example/tree/master/springboot-redis
参考资料
Spring boot中Redis的使用:http://www.ityouknow.com/springboot/2016/03/06/spring-boot-redis.html