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. asmdisk opened & asmdisk cached

    ASMDISK OPENED - Disk is present in the storage system and is being accessed by Automatic Storage Ma ...

  2. linux:什么是linux

    1>.linux是一套作业系统(linux就是核心与呼叫这两层),每一种作业系统都是在他专门的硬体机器上面运行的:linux是一个Open Source的作业系统,具有可移植性 2>.li ...

  3. lcd 图片

    硬件平台:mini2440 软件环境:UCOS2 .ADS1.2 . LCD彩色图片转换工具BMP_to_H工具bmp2h LCD彩色图片转换工具BMP_to_H工具文件夹下的使用说明 在S3C241 ...

  4. G面经prepare: Pattern Match

    设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...

  5. HTML_css样式表 样式属性 格式布局

    CSS(Cascading Style Sheet,叠层样式表),作用是美化HTML网页. /*注释区域*/此为注释语法 一.样式表 (一)样式表的分类 1.内联样式表 和HTML联合显示,控制精确, ...

  6. m球求n盒子问题

    球同盒同可空盒问题 #include <bits/stdc++.h> using namespace std; const int N = 25; int dp[N][N]; int ma ...

  7. uva 10755 - Garbage Heap

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  8. MES: ESB

    ESB定义了消息的收发和收发池,对于各种通讯方式定义了收发API,在收到信息后由eventBus来发布消息 ISender: public abstract interface ISender { p ...

  9. extjs 4.2 日期控件 选择时分秒功能

    因为不支持时分秒,然后在网上也找了一段时间的插件,但是感觉起来都不大方便,最后找一个插件,只需要引用js文件,然后修改类型,就可以实现extjs下面的datafield带时分秒功能了. 步骤: 只需要 ...

  10. MySQL 请选择合适的列! 转载(http://www.cnblogs.com/baochuan/archive/2012/05/23/2513224.html)

    点击图片,可查看大图.    介绍   情况:如果你的表结构设计不良或你的索引设计不佳,那么请你优化你的表结构设计和给予合适的索引,这样你的查询性能就能提高几个数量级.——数据越大,索引的价值越能体现 ...