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 = 0; 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 = 0; 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 = 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;
}
}

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

/**
* redis缓存list对象
*/
@Test
public void setCatchValueForList() {
String key = "user_Test7";
List<Code> codeList = new ArrayList<>();
Code code1 = new Code("436436", 533);
Code code2 = new Code("214214", 53453);
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设置有效时长的单位为小时

转自:https://www.cnblogs.com/baizhanshi/p/5527467.html

spring-redis 存储数据的更多相关文章

  1. redis存储数据的时候

    使用redis存储数据的时候,有时候为了查看的方便,通常会有层级或者说是目录, 这时候我们在set的时候,需要将key值使用“:”的符号来区分层级关系,比如:set(“a:b”, “123”),那么在 ...

  2. spring + redis 实现数据的缓存

    1.实现目标 通过redis缓存数据.(目的不是加快查询的速度,而是减少数据库的负担) 2.所需jar包 注意:jdies和commons-pool两个jar的版本是有对应关系的,注意引入jar包是要 ...

  3. redis存储数据

    redis存储结构--5种 RedisTemplate访问Redis数据结构(一)--String https://blog.csdn.net/qq_25135655/article/details/ ...

  4. 初识Redis系列之四:.net使用Redis存储数据

    首先Redis在Windows上的安装前面的文章已经介绍过,这里不介绍了,直奔主题, 直接来看看.net怎么使用Redis 首先需要引用redis相关的dll,两个途径,意识网上下载编译好的dll : ...

  5. 【JAVA】使用 jedis操作redis——连接、存储数据、切库等

    本篇运用Java调用jedis包(jedis在线文档API ),做简单操作实例. 安装jedis 1. 2.9.0 jar 版本下载: jedis-2.9.0.jar 2. 新建项目,添加该驱动包 连 ...

  6. spring+redis实现缓存

    spring + redis 实现数据的缓存 1.实现目标 通过redis缓存数据.(目的不是加快查询的速度,而是减少数据库的负担) 2.所需jar包 注意:jdies和commons-pool两个j ...

  7. Spring+redis整合遇到的问题集以及注意事项

    redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  8. Spring Boot使用redis做数据缓存

    1 添加redis支持 在pom.xml中添加 <dependency> <groupId>org.springframework.boot</groupId> & ...

  9. Spring Boot:使用Redis存储技术

    综合概述 Redis是一个开源免费的高性能key-value数据库,读取速度达110000次/s,写入速度达81000次/s.Redis支持丰富的数据类型,如Lists, Hashes, Sets 及 ...

  10. SpringBoot 结合 Spring Cache 操作 Redis 实现数据缓存

    系统环境: Redis 版本:5.0.7 SpringBoot 版本:2.2.2.RELEASE 参考地址: Redus 官方网址:https://redis.io/ 博文示例项目 Github 地址 ...

随机推荐

  1. Java中Return和Finally执行顺序的实现

    下面这段代码的执行结果是怎样的呢? publc int test(){ int x; try{ x = 1; return x; }catch(Exception e){ x = 2; return ...

  2. myeclipse 10破解

    因为笔者的电脑是刚买不久,忘记先给电脑分区,等软年安装差不多了才发现忘记分区,所以就备份了数据,然后分区,结果分区过程中没有异常发生,就没用备用数据,就用分过区的原数据,当时还以为没问题,结果打开my ...

  3. Call requires API level 7 (current min is 1):(问题解决)

    在一个导入的项目里修改加入webView的时候设置缩放属性的设置报错: Call requires API level 7 (current min is 1): android.webkit.Web ...

  4. C++ 私有构造函数的作用

    很多情况下要求当前的程序中只有一个object.例如一个程序只有一个和数据库的连接,只有一个鼠标的object.通常我们都将构造函数的声明置于public区段,假如我们将 其放入private区段中会 ...

  5. IFC文档结构说明

    工业基础类为代表的建筑信息BIM数据交换和共享在一个建筑或设施管理项目各参与者之间的开放规范的建模.IFC是国际openbim标准.本文件包含的IFC标准的规范.该规范包括的数据架构,表示为一个表达模 ...

  6. Boost log中的几个问题

    1. 使用动态库时,要定义 BOOST_LOG_DYN_LINK  或者 BOOST_ALL_DYN_LINK 否则会出现如下错误: CMakeFiles/xxxx.dir/xxxx.cpp.o: I ...

  7. 100723H Obfuscation

    传送门 题目大意 给你一个包含n 个单词的字典,给你一篇文章,文章包括若干词典里的单词,把句子里的空格都去掉,单词的首位字母都不变,中间的字符集为乱序,问能否恢复这篇文章,使得单词都是词典里的单词,如 ...

  8. 移动应用中的AR开发,5款最受欢迎工具推荐!

      英文原文:Top 5 Tools for Augmented Reality in Mobile Apps 还记得前段时间在网上很火的 3D 小熊不?托它的福,为相当一部分人科普了增强现实(AR) ...

  9. Javascript parseInt()和parseFloat()的用法

    parseInt()方法首先查看位置0处的 字符,判断它是否是个有效数字:如果不是,该方法将返回NaN,不再继续执行其他操作.但如果该字符是有效数字,该方法将查看位置1处的字符,进行同样的 测试.这一 ...

  10. vimrc 我的专属vim配置

    set nu set wrap syntax on filetype on "打开vim文件类型自动检测功能 set autoindent set smartindent set ruler ...