Redis工具類對string,list,hash,set,zset資料類型的操作。 添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
Redis配置
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.*;/** * Created by Administrator on 2017/7/2. * Redis模板配置,緩衝配置 */@Configuration@EnableCachingpublic class RedisConfig { @Autowired RedisConnectionFactory redisConnectionFactory; /** * 執行個體化 RedisTemplate 對象 */ @Bean public RedisTemplate<String, Object> functionDomainRedisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); initDomainRedisTemplate(redisTemplate, redisConnectionFactory); return redisTemplate; } /** * 設定資料存入 redis 的序列化方式 */ private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) { /*redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer(Object.class));*/ redisTemplate.setConnectionFactory(factory); } /** * 執行個體化 HashOperations 對象,可以使用 Hash 類型操作 */ @Bean public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForHash(); } /** * 執行個體化 ValueOperations 對象,可以使用 String 操作 */ @Bean public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForValue(); } /** * 執行個體化 ListOperations 對象,可以使用 List 操作 */ @Bean public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForList(); } /** * 執行個體化 SetOperations 對象,可以使用 Set 操作 */ @Bean public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForSet(); } /** * 執行個體化 ZSetOperations 對象,可以使用 ZSet 操作 */ @Bean public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForZSet(); } }
Redis操作
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.*;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.List;import java.util.Map;import java.util.Set;import java.util.concurrent.TimeUnit;@Servicepublic class RedisService { @Autowired private RedisTemplate<String, Object> redisTemplate; // HashMap操作 @Resource private HashOperations<String, String, Object> hashOperations; // Object操作 @Resource private ValueOperations<String, Object> valueOperations; // List操作 @Resource private ListOperations<String, Object> listOperations; // set操作 @Resource private SetOperations<String, Object> setOperations; // ZSet操作 @Resource private ZSetOperations<String, Object> zSetOperations; //--------------------------------------------------------------------- // redisTemplate //--------------------------------------------------------------------- /** * 判斷key是否存在 * @param key */ public boolean hasKey(String key) { return redisTemplate.hasKey(key); } /** * 刪除key * @param key */ public void delete(String key){ redisTemplate.delete(key); } /** * 判斷指定key的hashKey是否存在 * @param key * @param hashKey * @return */ public boolean hasKey(String key, String hashKey) { return redisTemplate.opsForHash().hasKey(key, hashKey); } /** * 設定逾時時間 * @param key * @param timeout * @param unit */ public void expire(String key, final long timeout, final TimeUnit unit) { redisTemplate.expire(key, timeout, unit); } /** * 擷取到期時間 * @param key * @return */ public long ttl(String key){ return redisTemplate.getExpire(key); } /** * 擷取指定pattern的key * @param pattern * @return */ public Set<String> keys(String pattern) { return redisTemplate.keys(pattern); } /** * 刪除多個key * @param keys */ public void delete(Set<String> keys) { redisTemplate.delete(keys); } /** * 設定到期時間 * @param key * @param expire */ private void setExpire (String key,long expire){ if (expire != -1) { redisTemplate.expire(key, expire, TimeUnit.SECONDS); } } //--------------------------------------------------------------------- // ValueOperations -> Redis String/Value 操作 //--------------------------------------------------------------------- /** * 設定key-value值 */ public void addValue(String key, Object value,long expire){ valueOperations.set(key, value); setExpire(key,expire); } /** * 設定key-value值,傳入時間單位 */ public void addValue(String key, Object value,long expire, TimeUnit timeUnit){ valueOperations.set(key, value, expire, timeUnit); } /** * 設定key-value值, 無到期時間 */ public void addValue(String key, Object value){ valueOperations.set(key, value); } /** * 擷取key的值 * */ public Object getValue(String key){ return valueOperations.get(key); } //--------------------------------------------------------------------- // HashOperations -> Redis Redis Hash 操作 //--------------------------------------------------------------------- /** * 向redis 中新增內容 * @param key 儲存key * @param hashKey hashKey * @param data 儲存對象 data * @param expire 到期時間 -1:表示不到期 */ public void addHashValue(String key,String hashKey, Object data, long expire) { hashOperations.put(key, hashKey, data); setExpire(key,expire); } /** * Hash 添加資料 * @param key key * @param map data */ public void addAllHashValue(String key, Map<String, Object> map, long expire) { hashOperations.putAll(key, map); setExpire(key,expire); } /** * 刪除hash key * @param key key * @param hashKey hashKey */ public long deleteHashValue(String key, String hashKey) { return hashOperations.delete(key, hashKey); } /** * 擷取資料 */ public Object getHashValue(String key, String hashKey) { return hashOperations.get(key, hashKey); } /** * 批量擷取資料 */ public List<Object> getHashAllValue(String key) { return hashOperations.values(key); } /** * 批量擷取指定hashKey的資料 */ public List<Object> getHashMultiValue(String key, List<String> hashKeys) { return hashOperations.multiGet(key, hashKeys); } /** * 擷取hash數量 */ public Long getHashCount(String key) { return hashOperations.size(key); } //--------------------------------------------------------------------- // ZSetOperations -> Redis Sort Set 操作 //--------------------------------------------------------------------- /** * 設定zset值 */ public boolean addZSetValue(String key, Object member, long score){ return zSetOperations.add(key, member, score); } /** * 設定zset值 */ public boolean addZSetValue(String key, Object member, double score){ return zSetOperations.add(key, member, score); } /** * 大量設定zset值 */ public long addBatchZSetValue(String key, Set<ZSetOperations.TypedTuple<Object>> tuples){ return zSetOperations.add(key, tuples); } /** * 自增zset值 */ public void incZSetValue(String key, String member, long delta){ zSetOperations.incrementScore(key, member, delta); } /** * 擷取zset數量 */ public long getZSetScore(String key, String member){ Double score = zSetOperations.score(key, member); if(score==null){ return 0; }else{ return score.longValue(); } } /** * 擷取有序集 key 中成員 member 的排名 。其中有序整合員按 score 值遞減 (從小到大) 排序。 */ public Set<ZSetOperations.TypedTuple<Object>> getZSetRank(String key, long start, long end){ return zSetOperations.rangeWithScores(key, start, end); } //--------------------------------------------------------------------- // listOperations -> Redis List() 操作 //--------------------------------------------------------------------- /** * 添加list列表 */ public void addListValue(String key,Object list){ listOperations.leftPush(key,list); } /** * 擷取指定Key對應的list */ public Object getListValue(String key){ return listOperations.leftPop(key); } //--------------------------------------------------------------------- // setOperations -> Redis Set() 操作 //--------------------------------------------------------------------- /** * 添加Set集合集合 */ public void addSetValue(String key,Object list){ setOperations.add(key,list); } /** * 擷取指定Key對應的set */ public Object getSetValue(String key){ return setOperations.members(key); } /** * 擷取並移除指定key的值 */ public Object popSetValue(String key){ return setOperations.pop(key); }}