spirng整合redis (二)
来源:互联网 发布:python布尔值 编辑:程序博客网 时间:2024/06/11 05:44
上一节spring整合redis的工具类,用到的redis模板为StringRedisTemplate,List和Map、Set等需要转换为String字符串后进行操作,spring提供了工具类
redis缓存工具类
ValueOperations ——基本数据类型和实体类的缓存
ListOperations ——list的缓存
SetOperations ——set的缓存
HashOperations ——Map的缓存
RedisTemplate模板配置
<bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"></property> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> </bean>
工具类
import java.io.Serializable;import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.data.redis.core.BoundSetOperations;import org.springframework.data.redis.core.HashOperations;import org.springframework.data.redis.core.ListOperations;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Service;@Servicepublic class RedisCacheUtil<T> { @Autowired @Qualifier("jedisTemplate") public RedisTemplate redisTemplate; /** * 缓存基本的对象,Integer、String、实体类等 * * @param key 缓存的键值 * @param value 缓存的值 * @return 缓存的对象 */ public <T> ValueOperations<String, T> setCacheObject(String key, T value) { ValueOperations<String, T> operation = redisTemplate.opsForValue(); operation.set(key, value); return operation; } /** * 获得缓存的基本对象。 * * @param key 缓存键值 * @return 缓存键值对应的数据 */ public <T> T getCacheObject(String key) { ValueOperations<String, T> operation = redisTemplate.opsForValue(); return operation.get(key); } /** * 缓存List数据 * * @param key 缓存的键值 * @param dataList 待缓存的List数据 * @return 缓存的对象 */ public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList) { ListOperations listOperation = redisTemplate.opsForList(); if (null != dataList) { int size = dataList.size(); for (int i = 0; i < size; i++) { listOperation.rightPush(key, dataList.get(i)); } } return listOperation; } /** * 获得缓存的list对象 * * @param key 缓存的键值 * @return 缓存键值对应的数据 */ public <T> List<T> getCacheList(String key) { List<T> dataList = new ArrayList<T>(); ListOperations<String, T> listOperation = redisTemplate.opsForList(); Long size = listOperation.size(key); for (int i = 0; i < size; i++) { dataList.add((T) listOperation.leftPop(key)); } return dataList; } /** * 缓存Set * * * @param key 缓存键值 * @param dataSet 缓存的数据 * @return 缓存数据的对象 */ public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet) { BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key); Iterator<T> it = dataSet.iterator(); while (it.hasNext()) { setOperation.add(it.next()); } return setOperation; } /** * 获得缓存的set * * @param key * @return */ public Set<T> getCacheSet(String key) { Set<T> dataSet = new HashSet<T>(); BoundSetOperations<String, T> operation = redisTemplate.boundSetOps(key); Long size = operation.size(); for (int i = 0; i < size; i++) { dataSet.add(operation.pop()); } return dataSet; } /** * 缓存Map * * @param key * @param dataMap * @return */ public <T> HashOperations<String, String, T> setCacheMap(String key, Map<String, T> dataMap) { HashOperations hashOperations = redisTemplate.opsForHash(); if (null != dataMap) { for (Map.Entry<String, T> entry : dataMap.entrySet()) { hashOperations.put(key, entry.getKey(), entry.getValue()); } } return hashOperations; } /** * 获得缓存的Map * * @param key * @return */ public <T> Map<String, T> getCacheMap(String key) { Map<String, T> map = redisTemplate.opsForHash().entries(key); return map; } /** * 缓存Map * * @param key * @param dataMap * @return */ public <T> HashOperations<String, Integer, T> setCacheIntegerMap(String key, Map<Integer, T> dataMap) { HashOperations hashOperations = redisTemplate.opsForHash(); if (null != dataMap) { for (Map.Entry<Integer, T> entry : dataMap.entrySet()) { hashOperations.put(key, entry.getKey(), entry.getValue()); } } return hashOperations; } /** * 获得缓存的Map * * @param key * @return */ public <T> Map<Integer, T> getCacheIntegerMap(String key) { Map<Integer, T> map = redisTemplate.opsForHash().entries(key); return map; }}
参考文章:http://www.cnblogs.com/holdouts/articles/5811118.html
阅读全文
0 0
- spirng整合redis (二)
- springboot整合redis(二)
- JMS整合Spirng
- spirng boot 整合mybatis
- spirng+redis 发布订阅
- Spring 整合 redis (二)
- spirng整合mybatis的事务(接口开发配置)
- Spirng 整合jsf backBean对象
- Spirng+redis 2、实现RedisConfiguration
- Spirng学习笔记之bean和BeanFactory(二)
- Spirng整合Quartz构建MAVEN工程
- activeMQ学习笔记 -- day03 整合spirng
- (16)spirng注解
- spring与redis整合(二)--采用spring-data-redis方式
- Redis使用二、redis整合spring(redisTemplate工具类)
- SpringBoot(三)整合Redis
- Redis与spring整合缓存的业务场景使用方法二(使用注解@Cacheable@CacheEvict)
- Velocity + log4j + Spring MVC + Spirng + MyBatis + Maven整合
- WPF项目中的问题归档
- python学习笔记一 关于python学习前了解的内容
- 使用brew进行Node.js Mac环境安装,示例LeanCloud
- 使用 PHP-CS-Fixer 规范PHP代码
- Linux下安装MySQL和卸载
- spirng整合redis (二)
- POJ 1177 线段树+扫描线
- 查询当前数据库的状态
- Overview of TensorFlow
- popuwindow升级版的更高层次
- 通过js实现单击或双击直接修改内容
- UOJ 手机的生产
- 中兴新支点上的终端与shell
- 以太坊智能合约安全监测工具 Oyente