Redis-Java 交互的应用
一、首先介绍一下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包
<!--共享Session-spring-session-data-redis分布式系统中,
sessiong共享有很多的解决方案,其中托管到缓存中应该是最常用的方案之一-->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
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 配置
@RestController public class RedisController { @Autowired private RedisServiceImpl service; //添加 @RequestMapping(value = "/add", method = RequestMethod.GET) public void test() { System.out.println("start....."); RedisModel m = new RedisModel(); m.setName("张三"); m.setTel("1111"); m.setAddress("深圳1"); m.setRedisKey("zhangsanKey01"); service.put(m.getRedisKey(), m, -1); RedisModel m2 = new RedisModel(); m2.setName("张三2"); m2.setTel("2222"); m2.setAddress("深圳2"); m2.setRedisKey("zhangsanKey02"); service.put(m2.getRedisKey(), m2, -1); RedisModel m3 = new RedisModel(); 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) @ResponseBody public Object getAll() { return service.getAll(); } //查询所有key @RequestMapping(value = "/getKeys", method = RequestMethod.GET) @ResponseBody public Object getKeys() { return service.getKeys(); } //根据key查询 @RequestMapping(value = "/get", method = RequestMethod.GET) public Object get(String key) { RedisModel m = new RedisModel(); m.setRedisKey(key); return service.get(m.getRedisKey()); } //删除 @RequestMapping(value = "/remove", method = RequestMethod.GET) @ResponseBody public void remove() { RedisModel m = new RedisModel(); m.setRedisKey("zhangsanKey01"); service.remove(m.getRedisKey()); } //判断key是否存在 @RequestMapping(value = "/isKeyExists", method = RequestMethod.GET) @ResponseBody public void isKeyExists() { RedisModel m = new RedisModel(); m.setRedisKey("zhangsanKey01"); boolean flag = service.isKeyExists(m.getRedisKey()); System.out.println("zhangsanKey01 是否存在: "+flag); } //查询当前缓存的数量 @RequestMapping(value = "/count", method = RequestMethod.GET) @ResponseBody public Object count() { return service.count(); } //清空所有key @RequestMapping(value = "/empty", method = RequestMethod.GET) @ResponseBody public void empty() { service.empty(); }
IRedisService 配置
public abstract class IRedisService<T> { @Autowired protected RedisTemplate<String, Object> redisTemplate; @Resource protected HashOperations<String, String, T> hashOperations; /** * 存入redis中的key * * @return */ protected abstract String getRedisKey(); /** * 添加 * * @param key key * @param doamin 对象 * @param expire 过期时间(单位:秒),传入 -1 时表示不设置过期时间 */ public void put(String key, T doamin, long expire) { hashOperations.put(getRedisKey(), key, doamin); if (expire != -1) { redisTemplate.expire(getRedisKey(), expire, TimeUnit.SECONDS); } } /** * 删除 * * @param key 传入key的名称 */ public void remove(String key) { hashOperations.delete(getRedisKey(), key); } /** * 查询 * * @param key 查询的key * @return */ public T get(String key) { return hashOperations.get(getRedisKey(), key); } /** * 获取当前redis库下所有对象 * * @return */ public List<T> getAll() { return hashOperations.values(getRedisKey()); } /** * 查询查询当前redis库下所有key * * @return */ public Set<String> getKeys() { return hashOperations.keys(getRedisKey()); } /** * 判断key是否存在redis中 * * @param key 传入key的名称 * @return */ public boolean isKeyExists(String key) { return hashOperations.hasKey(getRedisKey(), key); } /** * 查询当前key下缓存数量 * * @return */ public long count() { return hashOperations.size(getRedisKey()); } /** * 清空redis */ public void empty() { Set<String> set = hashOperations.keys(getRedisKey()); set.stream().forEach(key -> hashOperations.delete(getRedisKey(),key)); }
RedisServiceImpl 配置
@Service public class RedisServiceImpl extends IRedisService<RedisModel> { private static final String REDIS_KEY = "TEST_REDIS_KEY"; @Override protected String getRedisKey() { return this.REDIS_KEY; } }
RedisModel 配置
public class RedisModel implements Serializable { private String redisKey;//redis中的key private String name;//姓名 private String tel;//电话 private String address;//住址 public String getRedisKey() { return redisKey; } public void setRedisKey(String redisKey) { this.redisKey = redisKey; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }
RedisConfig配置
@Configuration public class RedisConfig { /** * 注入 RedisConnectionFactory */ @Autowired RedisConnectionFactory redisConnectionFactory; /** * 实例化 RedisTemplate 对象 * * @return */ @Bean public RedisTemplate<String, Object> functionDomainRedisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); initDomainRedisTemplate(redisTemplate, redisConnectionFactory); return redisTemplate; } /** * 设置数据存入 redis 的序列化方式 * * @param redisTemplate * @param factory */ private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) { redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setConnectionFactory(factory); } /** * 实例化 HashOperations 对象,可以使用 Hash 类型操作 * * @param redisTemplate * @return */ @Bean public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForHash(); } /** * 实例化 ValueOperations 对象,可以使用 String 操作 * * @param redisTemplate * @return */ @Bean public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForValue(); } /** * 实例化 ListOperations 对象,可以使用 List 操作 * * @param redisTemplate * @return */ @Bean public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForList(); } /** * 实例化 SetOperations 对象,可以使用 Set 操作 * * @param redisTemplate * @return */ @Bean public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForSet(); } /** * 实例化 ZSetOperations 对象,可以使用 ZSet 操作 * * @param redisTemplate * @return */ @Bean public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForZSet(); }
Redis 实现session 共享 sessionConfig 配置
@Configuration @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30) public class SessionConfig { }
调用方法
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
运行之后收工!!!!!!!!!!