package com.fndsoft.bcis.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service; import java.util.*; /**
* redis缓存帮助类
* Created by DELL on 2016/5/23.
*/
@Service
public class RedisCacheUtil<T> { @Autowired
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 = ; i < size; i++) {
listOperation.leftPush(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 = ; i < size; i++) {
dataList.add(listOperation.index(key,i));
}
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 = ; 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;
}
}

以下是存储list对象的测试方法:

 /**
* redis缓存list对象
*/
@Test
public void setCatchValueForList() {
String key = "user_Test7";
List<Code> codeList = new ArrayList<>();
Code code1 = new Code("", );
Code code2 = new Code("", );
codeList.add(code1);
codeList.add(code2);
redisCacheUtil.setCacheList(key, codeList);
System.out.println("保存数据成功!");
} @Test
public void getValueForList() {
String key = "user_Test7";
List<Code> codeList = redisCacheUtil.getCacheList(key);
for (Code code : codeList) {
System.out.println(code.getName() + " " + code.getAge());
}
}

切记被缓存的实例化对象必须系列化:

public class Code implements Serializable {}

另外还可以对将要保存的key设置有效时间,超过此有效时间,该key会自动删除失效:
redisTemplate.expire(key, 1, TimeUnit.HOURS);//此处TimeUnit.HOURS设置有效时长的单位为小时

  

redis 几种数据类型往数据库存数据和取数据的帮助类的更多相关文章

  1. 2 万字 + 20张图| 细说 Redis 九种数据类型和应用场景

    作者:小林coding 计算机八股文网(操作系统.计算机网络.计算机组成.MySQL.Redis):https://xiaolincoding.com 大家好,我是小林. 我们都知道 Redis 提供 ...

  2. redis五种数据类型的使用(zz)

    redis五种数据类型的使用 redis五种数据类型的使用 (摘自:http://tech.it168.com/a2011/0818/1234/000001234478_all.shtml ) 1.S ...

  3. redis五种数据类型的使用

    redis五种数据类型的使用 redis五种数据类型的使用 (摘自:http://tech.it168.com/a2011/0818/1234/000001234478_all.shtml ) 1.S ...

  4. redis五种数据类型的使用场景

    string 1.String 常用命令: 除了get.set.incr.decr mget等操作外,Redis还提供了下面一些操作: 获取字符串长度 往字符串append内容 设置和获取字符串的某一 ...

  5. Redis五种数据类型-设置key的过期时间

    1.redis命令客户端 [root@localhost bin]# ./redis-cli 127.0.0.1:6379> #是否运行着 127.0.0.1:6379> ping PON ...

  6. redis 五种数据类型的使用场景

    String 1.String 常用命令: 除了get.set.incr.decr mget等操作外,Redis还提供了下面一些操作: 获取字符串长度 往字符串append内容 设置和获取字符串的某一 ...

  7. [转]redis 五种数据类型的使用场景

    FROM : http://blog.csdn.net/gaogaoshan/article/details/41039581#t5 String 1.String 常用命令: 除了get.set.i ...

  8. Redis基础入门,Redis的优点也特点,Redis五种数据类型

    Redis是一个开源,高级的键值存储和一个适用的解决方案,用于构建高性能,可扩展的Web应用程序. 1.Redis的主要特点 Redis有三个主要特点,使它优越于其它键值数据存储系统 - Redis将 ...

  9. redis五种数据类型和常用命令及适用场景

    一.redis的5种数据类型: 1.基础理解: string 字符串(可以为整形.浮点型和字符串,统称为元素) list 列表(实现队列,元素不唯一,先入先出原则) set 集合(各不相同的元素) h ...

  10. redis 五种数据类型

    前言 前面学会了单机, 学会了集群, 但是redis咋用啊? 或者说, redis支持哪些数据类型呢? 常用的有五种: String , Hash, List, Set, zset(SortedSet ...

随机推荐

  1. web文件上传的实现

    1,html页面,上传使用input type=file控件,其所在的form必须加上enctype="multipart/form-data" <form role=&qu ...

  2. Android Bundle类

    根据google官方的文档(http://developer.android.com/reference/android/os/Bundle.html) Bundle类是一个key-value对,“A ...

  3. C# ?和??运算表达式

    1.单问号(?) 作用:用于给变量设初化的时候,给变量(int类型)赋为null值,而不是0. 例子: public int a; //默认值为0 public int ?b; //默认值为null ...

  4. redhat 6.7 安装nvidia显卡驱动时出现的问题

    一.给Redhat装Nvidia驱动时,出现类似ERROR: The Nouveau kernel driver is currently in use by your system. 的错误,这是应 ...

  5. java io读书笔记(2)什么是stream

    什么是stream?stream就是一个长度不确定的有序字节序列. Input streams move bytes of data into a Java program from some gen ...

  6. 设置UISegmentedControl中字体大小

    [segmentedControl setTitleTextAttributes:@{NSFontAttributeName : DYBoldFont(20)}  forState:UIControl ...

  7. SLF4J环境变量配置

    因部分程序需要,需要把SLF4J加入到环境变量中. 添加位置:CLASSPATH 添加信息如下: C:\slf4j-1.7.19\slf4j-nop-1.7.19.jar;

  8. srcolltop 的用法

    document.body.scrollTop用法 网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可 ...

  9. C#删除xml中某个节点的子节点方法

    if (File.Exists(xmlFilePath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFilePath); Xm ...

  10. UML: 协作图

    摘自http://www.umlonline.org/school/thread-38-1-1.html UML1.1时,协作图英文名字叫:Collaboration Diagram,UML2.0时, ...