Spring 集成 Redis
pom.xml
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.4.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
spring-redis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Redis Standalone 单节点配置 -->
<bean id="redisStandaloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="database" value="${redis.database}"/>
<property name="password">
<bean class="org.springframework.data.redis.connection.RedisPassword">
<constructor-arg index="0" value="${redis.password}"/>
</bean>
</property>
</bean>
<!-- Redis 连接配置 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="standaloneConfig" ref="redisStandaloneConfiguration"/>
</bean>
<!-- Redis 序列化 -->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="jsonRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer">
<!-- 使用默认的 ObjectMapper,移除JSON格式化后的 “@Class”节点 -->
<constructor-arg name="mapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper"/>
</constructor-arg>
</bean>
<!-- Redis 持久化模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="jsonRedisSerializer"/>
<property name="hashKeySerializer" ref="stringRedisSerializer"/>
<property name="hashValueSerializer" ref="jsonRedisSerializer"/>
</bean>
</beans>
spring-redis-sentine.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Redis 连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
<property name="testOnReturn" value="${redis.testOnReturn}"/>
</bean>
<!-- Redis Sentine 哨兵集群配置 -->
<bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<constructor-arg index="0" value="${redis.master}"/>
<constructor-arg index="1">
<set>
<value>${redis.sentine.1}</value>
<value>${redis.sentine.2}</value>
<value>${redis.sentine.3}</value>
</set>
</constructor-arg>
<property name="database" value="${redis.database}"/>
<property name="password">
<bean class="org.springframework.data.redis.connection.RedisPassword">
<constructor-arg index="0" value="${redis.password}"/>
</bean>
</property>
</bean>
<!-- Redis 连接配置 -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
<constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration"/>
</bean>
<!-- Redis 序列化 -->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="jsonRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer">
<!-- 使用默认的 ObjectMapper,移除JSON格式化后的 “@Class”节点 -->
<constructor-arg name="mapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper"/>
</constructor-arg>
</bean>
<!-- Redis 持久化模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="jsonRedisSerializer"/>
<property name="hashKeySerializer" ref="stringRedisSerializer"/>
<property name="hashValueSerializer" ref="jsonRedisSerializer"/>
</bean>
</beans>
RedisUtil.java
package com.app.core.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
/**
* Spring Redis 基础操作模板
*/
private static RedisTemplate<String, Object> redisTemp;
/**
* Redis 数据类型-字符串
*/
private static ValueOperations<String, Object> valueOps;
/**
* Redis 数据类型-字典
*/
private static HashOperations<String, String, Object> hashOps;
/**
* Redis 数据类型-列表
*/
private static ListOperations<String, Object> listOps;
/**
* Redis 数据类型-集合
*/
private static SetOperations<String, Object> setOps;
/**
* Redis 数据类型-有序集合
*/
private static ZSetOperations<String, Object> zSetOps;
@Autowired
public void setRedisTemp(RedisTemplate<String, Object> redisTemp) {
RedisUtil.redisTemp = redisTemp;
}
@Resource(name = "redisTemplate")
public void setValueOps(ValueOperations<String, Object> valueOps) {
RedisUtil.valueOps = valueOps;
}
@Resource(name = "redisTemplate")
public void setHashOps(HashOperations<String, String, Object> hashOps) {
RedisUtil.hashOps = hashOps;
}
@Resource(name = "redisTemplate")
public void setListOps(ListOperations<String, Object> listOps) {
RedisUtil.listOps = listOps;
}
@Resource(name = "redisTemplate")
public void setSetOps(SetOperations<String, Object> setOps) {
RedisUtil.setOps = setOps;
}
@Resource(name = "redisTemplate")
public void setzSetOps(ZSetOperations<String, Object> zSetOps) {
RedisUtil.zSetOps = zSetOps;
}
/**
* 获取Spring Redis公共操作模板
*
* @return
*/
public static RedisTemplate<String, Object> getRedisTemp() {
return redisTemp;
}
/**
* 获取默认序列化器
* <p>默认序列化器为 JdkSerializationRedisSerializer</p>
*
* @return
*/
public static RedisSerializer<?> getDefaultSlz() {
return redisTemp.getDefaultSerializer();
}
/**
* 获取字符串序列化器
*
* @return
*/
public static RedisSerializer<String> getStringSlz() {
return redisTemp.getStringSerializer();
}
/**
* 获取缓存键序列化器
*
* @return
*/
public static RedisSerializer<?> getKeySlz() {
return redisTemp.getKeySerializer();
}
/**
* 获取缓存值序列化器
*
* @return
*/
public static RedisSerializer<?> getValueSlz() {
return redisTemp.getValueSerializer();
}
/**
* 获取字典缓存键序列化器
*
* @return
*/
public static RedisSerializer<?> getHashKeySlz() {
return redisTemp.getHashKeySerializer();
}
/**
* 获取字典缓存值序列化器
*
* @return
*/
public static RedisSerializer<?> getHashValueSlz() {
return redisTemp.getHashValueSerializer();
}
/**
* 获取字符串操作模板
*
* @return
*/
public static ValueOperations<String, Object> getValueOps() {
return valueOps;
}
/**
* 获取字典操作模板
*
* @return
*/
public static HashOperations<String, String, Object> getHashOps() {
return hashOps;
}
/**
* 获取列表操作模板
*
* @return
*/
public static ListOperations<String, Object> getListOps() {
return listOps;
}
/**
* 获取集合操作模板
*
* @return
*/
public static SetOperations<String, Object> getSetOps() {
return setOps;
}
/**
* 获取有序集合操作模板
*
* @return
*/
public static ZSetOperations<String, Object> getzSetOps() {
return zSetOps;
}
/**
* 发布消息
*
* @param channel 发布频道
* @param message 消息内容
*/
public static void publish(String channel, Object message) {
redisTemp.convertAndSend(channel, message);
}
/**
* 获取所有缓存键
* <p>通过正则表达式匹配</p>
*
* @param pattern
* @return
*/
public static Set<String> keys(String pattern) {
return redisTemp.keys(pattern);
}
/**
* 单个删除
*
* @param key
*/
public static void delete(String key) {
redisTemp.delete(key);
}
/**
* 批量删除
*
* @param keys
*/
public static void deleteAll(Collection<String> keys) {
redisTemp.delete(keys);
}
/**
* 批量删除
* <p>通过正则表达式匹配</p>
*
* @param pattern
*/
public static void deleteAll(String pattern) {
redisTemp.delete(keys(pattern));
}
/**
* 字符串-单个存值
*
* @param key
* @param value
*/
public static void valueSet(String key, Object value) {
valueOps.set(key, value);
}
/**
* 字符串-单个定时存值
*
* @param key
* @param value
* @param timeout 有效时间
* @param unit 时间单位
*/
public static void valueSet(String key, Object value, long timeout, TimeUnit unit) {
valueOps.set(key, value, timeout, unit);
}
/**
* 字符串-批量存值
*
* @param map
*/
public static void valueMultiSet(Map<String, Object> map) {
valueOps.multiSet(map);
}
/**
* 字符串-单个取值
*
* @param key
* @return
*/
public static Object valueGet(String key) {
return valueOps.get(key);
}
/**
* 字符串-批量取值
*
* @param keys
* @return
*/
public static List<Object> valueMultiGet(Collection<String> keys) {
return valueOps.multiGet(keys);
}
/**
* 字符串-批量取值
* <p>通过正则表达式匹配</p>
*
* @param pattern
* @return
*/
public static List<Object> valueMultiGet(String pattern) {
return valueOps.multiGet(keys(pattern));
}
/**
* 字典-单个存值
*
* @param key
* @param hashKey
* @param value
*/
public static void hashPut(String key, String hashKey, Object value) {
hashOps.put(key, hashKey, value);
}
/**
* 字典-批量存值
*
* @param key
* @param map
*/
public static void hashPutAll(String key, Map<String, Object> map) {
hashOps.putAll(key, map);
}
/**
* 字典-单个取值
*
* @param key
* @param hashKey
* @return
*/
public static Object hashGet(String key, String hashKey) {
return hashOps.get(key, hashKey);
}
/**
* 字典-批量取值
*
* @param key
* @param hashKeys
* @return
*/
public static List<Object> hashMultiGet(String key, Collection<String> hashKeys) {
return hashOps.multiGet(key, hashKeys);
}
/**
* 字典-遍历键和值
*
* @param key
* @return
*/
public static Map<String, Object> hashEntries(String key) {
return hashOps.entries(key);
}
/**
* 字典-遍历键
*
* @param key
* @return
*/
public static Set<String> hashKeys(String key) {
return hashOps.keys(key);
}
/**
* 字典-遍历值
*
* @param key
* @return
*/
public static List<Object> hashValues(String key) {
return hashOps.values(key);
}
/**
* 字典-删除
*
* @param key
* @param hashKeys
*/
public static void hashDelete(String key, Object... hashKeys) {
hashOps.delete(key, hashKeys);
}
/**
* 列表-左边单个存值
*
* @param key
* @param value
* @return
*/
public static Long listLeftPush(String key, Object value) {
return listOps.leftPush(key, value);
}
/**
* 列表-左边批量存值
*
* @param key
* @param values
* @return
*/
public static Long listLeftPushAll(String key, Collection<Object> values) {
return listOps.leftPushAll(key, values);
}
/**
* 列表-右边单个存值
*
* @param key
* @param value
* @return
*/
public static Long listRightPush(String key, Object value) {
return listOps.rightPush(key, value);
}
/**
* 列表-右边批量存值
*
* @param key
* @param values
* @return
*/
public static Long listRightPushAll(String key, Collection<Object> values) {
return listOps.rightPushAll(key, values);
}
/**
* 列表-指定下标更新
*
* @param key
* @param index
* @param value
*/
public static void listSet(String key, long index, Object value) {
// 先判断是否存在当前下标的元素
if (listOps.size(key) > index)
listOps.set(key, index, value);
}
/**
* 列表-左边取值
*
* @param key
* @return
*/
public static Object listLeftPop(String key) {
return listOps.leftPop(key);
}
/**
* 列表-右边取值
*
* @param key
* @return
*/
public static Object listRightPop(String key) {
return listOps.rightPop(key);
}
/**
* 列表-指定下标取值
*
* @param key
* @param index
* @return
*/
public static Object listIndex(String key, long index) {
return listOps.index(key, index);
}
/**
* 列表-指定下标区间查看(不取值)
*
* @param key
* @param start
* @param end
* @return
*/
public static List<Object> listRange(String key, long start, long end) {
return listOps.range(key, start, end);
}
/**
* 列表-查看所有列表值(不取值)
*
* @param key
* @return
*/
public static List<Object> listRangeAll(String key) {
return listOps.range(key, 0, listOps.size(key));
}
/**
* 列表-指定下标移除
*
* @param key
* @param index
* @param value
* @return
*/
public static Long listRemove(String key, long index, Object value) {
return listOps.remove(key, index, value);
}
/**
* 集合-添加一个或多个元素
*
* @param key
* @param values
* @return
*/
public static Long setAdd(String key, Object... values) {
return setOps.add(key, values);
}
/**
* 集合-移动集合元素
*
* @param key
* @param value
* @param destKey
* @return
*/
public static Boolean setMove(String key, Object value, String destKey) {
return setOps.move(key, value, destKey);
}
/**
* 集合-判断是否存在集合元素
*
* @param key
* @param value
* @return
*/
public static Boolean setIsMember(String key, Object value) {
return setOps.isMember(key, value);
}
/**
* 集合-遍历集合元素
*
* @param key
* @return
*/
public static Set<Object> setMembers(String key) {
return setOps.members(key);
}
/**
* 集合-多个集合的元素交集
*
* @param key
* @param otherKey
* @return
*/
public static Set<Object> setIntersect(String key, String otherKey) {
return setOps.intersect(key, otherKey);
}
/**
* 集合-多个集合的元素并集
*
* @param key
* @param otherKey
* @return
*/
public static Set<Object> setUnion(String key, String otherKey) {
return setOps.union(key, otherKey);
}
/**
* 集合-多个集合的元素差集
*
* @param key
* @param otherKey
* @return
*/
public static Set<Object> setDifference(String key, String otherKey) {
return setOps.difference(key, otherKey);
}
/**
* 集合-移除一个或多个元素
*
* @param key
* @param values
* @return
*/
public static Long setRemove(String key, Object... values) {
return setOps.remove(key, values);
}
/**
* 有序集合-添加
*
* @param key
* @param value
* @param score
* @return
*/
public static Boolean zSetAdd(String key, Object value, double score) {
return zSetOps.add(key, value, score);
}
/**
* 有序集合-按下标顺序查询
*
* @param key
* @param start
* @param end
* @return
*/
public static Set<Object> zSetRange(String key, long start, long end) {
return zSetOps.range(key, start, end);
}
/**
* 有序集合-按下标倒序查询
*
* @param key
* @param start
* @param end
* @return
*/
public static Set<Object> zSetReverseRange(String key, long start, long end) {
return zSetOps.reverseRange(key, start, end);
}
/**
* 有序集合-按分数顺序查询
*
* @param key
* @param min
* @param max
* @return
*/
public static Set<Object> zSetRangeByScore(String key, double min, double max) {
return zSetOps.rangeByScore(key, min, max);
}
/**
* 有序集合-按分数倒序查询
*
* @param key
* @param min
* @param max
* @return
*/
public static Set<Object> zSetReverseRangeByScore(String key, double min, double max) {
return zSetOps.reverseRangeByScore(key, min, max);
}
/**
* 有序集合-顺序查询集合元素的下标
*
* @param key
* @param value
* @return
*/
public static Long zSetRank(String key, Object value) {
return zSetOps.rank(key, value);
}
/**
* 有序集合-倒序查询集合元素的下标
*
* @param key
* @param value
* @return
*/
public static Long zSetReverseRank(String key, Object value) {
return zSetOps.reverseRank(key, value);
}
/**
* 有序集合-查询集合元素的分数
*
* @param key
* @param value
* @return
*/
public static Double zSetScore(String key, Object value) {
return zSetOps.score(key, value);
}
/**
* 有序集合-批量删除
*
* @param key
* @param values
* @return
*/
public static Long zSetRemove(String key, Object... values) {
return zSetOps.remove(key, values);
}
/**
* 有序集合-按下标批量删除
*
* @param key
* @param start
* @param end
* @return
*/
public static Long zSetRemoveRange(String key, long start, long end) {
return zSetOps.removeRange(key, start, end);
}
/**
* 有序集合-按分数批量删除
*
* @param key
* @param min
* @param max
* @return
*/
public static Long zSetRemoveRangeByScore(String key, double min, double max) {
return zSetOps.removeRangeByScore(key, min, max);
}
}
config.properties
# spring-redis
redis.host=127.0.0.1
redis.port=8080
redis.database=0
redis.password=
# spring-redis-sentine
redis.maxTotal=
redis.maxIdle=
redis.maxWaitMillis=
redis.testOnBorrow=
redis.testOnReturn=
redis.master=
redis.sentine.1=
redis.sentine.2=
redis.sentine.3=
redis.database=
redis.password=
Spring 集成 Redis的更多相关文章
- Spring集成Redis集群(含spring集成redis代码)
代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...
- spring 集成 redis -- pub/sub
redis除了常用的当做缓存外,还可以当做简单的消息中间件,实现消息发布订阅 spring集成redis,可以使用spring-data-redis 首先引入相关maven依赖(此处我spring相关 ...
- spring集成redis
redis是一种非关系型数据库,与mongoDB不同的是redis是内存数据库,所以访问速度很快.常用作缓存和发布-订阅式的消息队列.redis官方没有提供windows版本的软件.windows版本 ...
- spring 集成redis客户端jedis(java)
spring集成jedis简单实例 jedis是redis的java客户端,spring将redis连接池作为一个bean配置. “redis.clients.jedis.JedisPool”,这 ...
- spring集成redis,集成redis集群
原文:http://chentian114.iteye.com/blog/2292323 1.通过spring-data-redis集成redis pom.xml依赖包 <project xml ...
- Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)
说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...
- spring集成redis——主从配置以及哨兵监控
Redis主从模式配置: Redis的主从模式配置是非常简单的,首先我们需要有2个可运行的redis环境: master node : 192.168.56.101 8887 slave node: ...
- Spring集成Redis缓存
作者:13 GItHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 整合Redis 本来以为类似的Redis教程和整合代码应该会很多,因 ...
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十二)Spring集成Redis缓存
作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 整合Redis 本来以为类似的Redis教程和整合代码应该会很多,因 ...
- Spring集成Redis使用注解
转载:http://blog.csdn.net/u013725455/article/details/52129283 使用Maven项目,添加jar文件依赖: <project xmlns=& ...
随机推荐
- BZOJ1912:[APIO2010]patrol巡逻
Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示村庄a与b之间有一条道路(1 ≤ a, b ≤ n). Ou ...
- Ubuntu clion下载及激活
1.下载 方法:去官网下载clion https://www.jetbrains.com/clion/download/#section=linux 或者使用我上传的百度网盘链接: https:// ...
- LUOGU P1654 OSU! (概率期望)
传送门 解题思路 首先考虑对于一个点来说,如果这个点是1的话,那么对于答案来说 $(ans+1)^3=ans^3+3*ans^2+3*ans+1$,这对于上一个答案来说其实贡献了 $3*ans^2+3 ...
- pb_ds(平板电视)简介
据说NOI赛制可以用pbds,故整理常用方法: 1.splay 所需声明及头文件: #include <ext/pb_ds/tree_policy.hpp> #include <ex ...
- 怎样理解js数组中indexOf()的用法与lastIndexOf
第一首先你运行一下它的js代码: var arr1=["大学","中庸","论语","孟子","诗" ...
- vue-cli 本地环境 API 代理设置和解决跨域
前言 我们在使用 vue-cli 启动项目的时候npm run dev便可以启动我们的项目了,通常我们的请求地址是以 localhost: 来请求接口数据的,localhost 是没有办法设置 coo ...
- 06_Hibernate缓存
一.缓存概述 什么是缓存: 缓存将数据库/硬盘上文件中数据,放入到缓存中(就是内存中一块空间).当再次使用的使用,可以直接从内存中获取. 缓存的好处: 提升程序运行的效率.缓存技术是Hibernate ...
- 01_Hibernate持久化
一.简介 思考:为什么使用Hibernate? Hibernate对JDBC访问数据库的代码进行了封装. Hibernate是一个基于JDBC的主流持久化框架. Hibernate的性能比较好,它是一 ...
- RQNOJ--160 竞赛真理(01背包)
题目http://www.rqnoj.cn/problem/160 分析:这是一个01背包问题,对于每一道题目,都有两个选择"做"或者"不做". 但是唯一不同的 ...
- CAS增加免登陆(Remember Me)功能
1. 打开deployerConfigContext.xml 在 authenticationManager 的bean中增加 <property name="authenticati ...