验证中...
码云 Gitee IDE 全新上线——支持 Git 管理的轻量在线编码环境
语言: Java
分类: Web开发技术
最后更新于 2018-03-15 10:12
片段 1 片段 2 片段 3 片段 4 片段 5
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
原始数据 复制代码
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
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=

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助