一、首先介绍一下Redis
Redis是目前业界使用最广泛的内存数据存储。相比memcached,Redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化。除此之外,Redis还提供一些类数据库的特性,比如事务,HA,主从库。可以说Redis兼具了缓存系统和数据库的一些特性,因此有着丰富的应用场景。本文介绍Redis在Spring Boot中典型的应用场景。
Redis缓存数据类型有5种,分别是String(字符串)、List(列表)、Hash(哈希)、Set(无序,不重复集合)、ZSet(sorted set:有序,不重复集合)。
二、先下载redis 我这里使用的是window64 版本
这是教程链接: http://blog.csdn.net/jinwufeiyang/article/details/52156817
Redis 下载链接 :https://github.com/MicrosoftArchive/redis/releases
双击图上的标记的 ,如成功便如图下
三、下面介绍java如何配置
这是我的项目结构
首先pom中引入jar包
org.springframework.session
spring-session-data-redis
application.properties 配置
# default-store in spring session. it will be set in redis only outside.
spring.session.store-type=none
# REDIS (RedisProperties)
# 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
RedisController 配置
@RestControllerpublic classRedisController {
@AutowiredprivateRedisServiceImpl service;//添加
@RequestMapping(value = "/add", method =RequestMethod.GET)public voidtest() {
System.out.println("start.....");
RedisModel m= newRedisModel();
m.setName("张三");
m.setTel("1111");
m.setAddress("深圳1");
m.setRedisKey("zhangsanKey01");
service.put(m.getRedisKey(), m,-1);
RedisModel m2= newRedisModel();
m2.setName("张三2");
m2.setTel("2222");
m2.setAddress("深圳2");
m2.setRedisKey("zhangsanKey02");
service.put(m2.getRedisKey(), m2,-1);
RedisModel m3= newRedisModel();
m3.setName("张三3");
m3.setTel("2222");
m3.setAddress("深圳2");
m3.setRedisKey("zhangsanKey03");
service.put(m3.getRedisKey(), m3,-1);
System.out.println("add success end...");
}//查询所有对象
@RequestMapping(value = "/getAll", method =RequestMethod.GET)
@ResponseBodypublicObject getAll() {returnservice.getAll();
}//查询所有key
@RequestMapping(value = "/getKeys", method =RequestMethod.GET)
@ResponseBodypublicObject getKeys() {returnservice.getKeys();
}//根据key查询
@RequestMapping(value = "/get", method =RequestMethod.GET)publicObject get(String key) {
RedisModel m= newRedisModel();
m.setRedisKey(key);returnservice.get(m.getRedisKey());
}//删除
@RequestMapping(value = "/remove", method =RequestMethod.GET)
@ResponseBodypublic voidremove() {
RedisModel m= newRedisModel();
m.setRedisKey("zhangsanKey01");
service.remove(m.getRedisKey());
}//判断key是否存在
@RequestMapping(value = "/isKeyExists", method =RequestMethod.GET)
@ResponseBodypublic voidisKeyExists() {
RedisModel m= newRedisModel();
m.setRedisKey("zhangsanKey01");boolean flag =service.isKeyExists(m.getRedisKey());
System.out.println("zhangsanKey01 是否存在: "+flag);
}//查询当前缓存的数量
@RequestMapping(value = "/count", method =RequestMethod.GET)
@ResponseBodypublicObject count() {returnservice.count();
}//清空所有key
@RequestMapping(value = "/empty", method =RequestMethod.GET)
@ResponseBodypublic voidempty() {
service.empty();
}
IRedisService 配置
public abstract class IRedisService{
@Autowiredprotected RedisTemplateredisTemplate;
@Resourceprotected HashOperationshashOperations;/*** 存入redis中的key
*
*@return
*/
protected abstractString getRedisKey();/*** 添加
*
*@paramkey key
*@paramdoamin 对象
*@paramexpire 过期时间(单位:秒),传入 -1 时表示不设置过期时间*/
public void put(String key, T doamin, longexpire) {
hashOperations.put(getRedisKey(), key, doamin);if (expire != -1) {
redisTemplate.expire(getRedisKey(), expire, TimeUnit.SECONDS);
}
}/*** 删除
*
*@paramkey 传入key的名称*/
public voidremove(String key) {
hashOperations.delete(getRedisKey(), key);
}/*** 查询
*
*@paramkey 查询的key
*@return
*/
publicT get(String key) {returnhashOperations.get(getRedisKey(), key);
}/*** 获取当前redis库下所有对象
*
*@return
*/
public ListgetAll() {returnhashOperations.values(getRedisKey());
}/*** 查询查询当前redis库下所有key
*
*@return
*/
public SetgetKeys() {returnhashOperations.keys(getRedisKey());
}/*** 判断key是否存在redis中
*
*@paramkey 传入key的名称
*@return
*/
public booleanisKeyExists(String key) {returnhashOperations.hasKey(getRedisKey(), key);
}/*** 查询当前key下缓存数量
*
*@return
*/
public longcount() {returnhashOperations.size(getRedisKey());
}/*** 清空redis*/
public voidempty() {
Set set =hashOperations.keys(getRedisKey());
set.stream().forEach(key->hashOperations.delete(getRedisKey(),key));
}
RedisServiceImpl 配置
@Servicepublic class RedisServiceImpl extends IRedisService{private static final String REDIS_KEY = "TEST_REDIS_KEY";
@OverrideprotectedString getRedisKey() {return this.REDIS_KEY;
}
}
RedisModel 配置
public class RedisModel implementsSerializable {private String redisKey;//redis中的key
private String name;//姓名
private String tel;//电话
private String address;//住址
publicString getRedisKey() {returnredisKey;
}public voidsetRedisKey(String redisKey) {this.redisKey =redisKey;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getTel() {returntel;
}public voidsetTel(String tel) {this.tel =tel;
}publicString getAddress() {returnaddress;
}public voidsetAddress(String address) {this.address =address;
}
RedisConfig配置
@Configurationpublic classRedisConfig {/*** 注入 RedisConnectionFactory*/@Autowired
RedisConnectionFactory redisConnectionFactory;/*** 实例化 RedisTemplate 对象
*
*@return
*/@Beanpublic RedisTemplatefunctionDomainRedisTemplate() {
RedisTemplate redisTemplate = new RedisTemplate<>();
initDomainRedisTemplate(redisTemplate, redisConnectionFactory);returnredisTemplate;
}/*** 设置数据存入 redis 的序列化方式
*
*@paramredisTemplate
*@paramfactory*/
private void initDomainRedisTemplate(RedisTemplateredisTemplate, RedisConnectionFactory factory) {
redisTemplate.setKeySerializer(newStringRedisSerializer());
redisTemplate.setHashKeySerializer(newStringRedisSerializer());
redisTemplate.setHashValueSerializer(newJdkSerializationRedisSerializer());
redisTemplate.setValueSerializer(newJdkSerializationRedisSerializer());
redisTemplate.setConnectionFactory(factory);
}/*** 实例化 HashOperations 对象,可以使用 Hash 类型操作
*
*@paramredisTemplate
*@return
*/@Beanpublic HashOperations hashOperations(RedisTemplateredisTemplate) {returnredisTemplate.opsForHash();
}/*** 实例化 ValueOperations 对象,可以使用 String 操作
*
*@paramredisTemplate
*@return
*/@Beanpublic ValueOperations valueOperations(RedisTemplateredisTemplate) {returnredisTemplate.opsForValue();
}/*** 实例化 ListOperations 对象,可以使用 List 操作
*
*@paramredisTemplate
*@return
*/@Beanpublic ListOperations listOperations(RedisTemplateredisTemplate) {returnredisTemplate.opsForList();
}/*** 实例化 SetOperations 对象,可以使用 Set 操作
*
*@paramredisTemplate
*@return
*/@Beanpublic SetOperations setOperations(RedisTemplateredisTemplate) {returnredisTemplate.opsForSet();
}/*** 实例化 ZSetOperations 对象,可以使用 ZSet 操作
*
*@paramredisTemplate
*@return
*/@Beanpublic ZSetOperations zSetOperations(RedisTemplateredisTemplate) {returnredisTemplate.opsForZSet();
}
Redis 实现session 共享 sessionConfig 配置
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds= 86400*30)public classSessionConfig {
}
调用方法
HttpSession session;
session.setAttribute("data", "Session 共享 9966");
String name= "端口号:9966========="+ session.getAttribute("data");
分别运行两个一样的配置 session 就共享了
源码 良心制作 :https://gitee.com/xdymemory00/FuZaiJunHeng_Redis_session-GongXiang
nginx +Redis window64 版本工具:https://gitee.com/xdymemory00/Redis_Nginx-GongJu.git
运行之后收工!!!!!!!!!!