redisTemplate操作手册
package common.redis.utils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Component
public class StringJedisUtil {
private Logger logger = LoggerFactory.getLogger(StringJedisUtil.class);
@Autowired
private RedisTemplate<String,Object> redisTemplate;
public StringJedisUtil(RedisTemplate<String,Object> redisTemplate){
this.redisTemplate = redisTemplate;
}
public boolean expire(String key, long time, TimeUnit timeUnit){
Boolean expire = false;
try{
if(time>0){
expire = redisTemplate.expire(key, time, timeUnit);
}
}catch (Exception e){
e.printStackTrace();
}
return expire;
}
public Boolean expire(String key, Date date){
boolean expireStatus = false;
try {
if(StringUtils.isNotBlank(key)){
expireStatus = redisTemplate.expireAt(key,date);
}
}catch (Exception e){
e.printStackTrace();
}
return expireStatus;
}
public Long getExpires(String key) {
return redisTemplate.getExpire(key);
}
public long getExpres(String key,TimeUnit timeUnit){
return redisTemplate.getExpire(key,timeUnit);
}
public Boolean hasKey(String key){
return redisTemplate.hasKey(key);
}
public Object getVal(String key){
return redisTemplate.opsForValue().get(key);
}
public void delete(String... key){
if(key.length > 0 && key != null){
if(key.length == 1){
redisTemplate.delete(key[0]);
}else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
public byte[] dump(String key){
return redisTemplate.dump(key);
}
public Set<String> getPatternKey(String patten) {
return redisTemplate.keys(patten);
}
public void renameKey(String oldKey,String newKey){
redisTemplate.rename(oldKey,newKey);
}
public DataType getKeyType(String key){
return redisTemplate.type(key);
}
public Boolean renameIfAbsent(String oldKey,String newKey){
return redisTemplate.renameIfAbsent(oldKey,newKey);
}
public String randomKey(){
return redisTemplate.randomKey();
}
public Boolean persistKey(String key){
return redisTemplate.persist(key);
}
public Boolean move(String key,Integer dataIndex){
return redisTemplate.move(key,dataIndex);
}
public void setKey(String key,Object val){
redisTemplate.opsForValue().set(key,val);
}
public void setValExpire(String key,Object val,Long time,TimeUnit unit){
redisTemplate.opsForValue().set(key,val,time,unit);
}
public String getCharacterRange(String key,Integer start,Integer end){
return redisTemplate.opsForValue().get(key,start,end);
}
public List<Object> multiGet(Collection<String> keys){
return redisTemplate.opsForValue().multiGet(keys);
}
public void append(String key,String val){
redisTemplate.opsForValue().append(key, val);
}
public Double incrDouble(String key,Double increment){
return redisTemplate.opsForValue().increment(key,increment);
}
public Long incrByLong(String key,Integer increment){
return redisTemplate.opsForValue().increment(key,increment);
}
public long getKeyLen(String key){
return redisTemplate.opsForValue().size(key);
}
public void multiSet(Map map){
redisTemplate.opsForValue().multiSet(map);
}
public boolean multiSetIfAbsent(Map map){
return redisTemplate.opsForValue().multiSetIfAbsent(map);
}
public Object getAndSet(String key,Object val){ return redisTemplate.opsForValue().getAndSet(key,val);}
}
package common.redis.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class ListJedisUtil {
@Autowired
private RedisTemplate redisTemplate;
public long lPush(String key,Object val){
return redisTemplate.opsForList().leftPush(key, val);
}
public long lPushAll(String key,Object... val){
return redisTemplate.opsForList().leftPushAll(key,val);
}
public long lPushAll(String key, Collection collection){
return redisTemplate.opsForList().leftPushAll(key,collection);
}
public long l(String key,Object val){
return redisTemplate.opsForList().leftPushIfPresent(key,val);
}
public long lPush(String key,Object val,Object val1){
return redisTemplate.opsForList().leftPush(key,val,val1);
}
public long rPush(String key,Object val){return redisTemplate.opsForList().rightPush(key,val);}
public long rPush(String key,Object... val){
return redisTemplate.opsForList().leftPushAll(key,val);
}
public long rPushAll(String key,Collection collection){
return redisTemplate.opsForList().leftPushAll(key,collection);
}
public Object index(String key,long index){
return redisTemplate.opsForList().index(key,index);
}
public List rangList(String key,int start,int end){
return redisTemplate.opsForList().range(key,start,end);
}
public long listLen(String key){
return redisTemplate.opsForList().size(key);
}
public void setListVal(String key,int index,Object val){
redisTemplate.opsForList().set(key,index,val);
}
public void remove(String key, String value, int count) {
redisTemplate.opsForList().remove(key, count, value);
}
public void trim(String key, Integer start, Integer end) {
redisTemplate.opsForList().trim(key, start, end);
}
public Object leftPop(String key){
return redisTemplate.opsForList().leftPop(key);
}
public Object leftPop(String key, Long time, TimeUnit unit){
return redisTemplate.opsForList().leftPop(key,time,unit);
}
public Object rightPop(String key){
return redisTemplate.opsForList().rightPop(key);
}
public Object rightPop(String key,Long time,TimeUnit unit){
return redisTemplate.opsForList().rightPop(key,time,unit);
}
}
```cpp
package common.redis.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
import java.util.Set;
public class SetJedisUtil {
private static Logger logger = LoggerFactory.getLogger(SetJedisUtil.class);
@Autowired
private RedisTemplate redisTemplate;
public long addSet(String key,Object... val){
long size = 0;
try {
size = redisTemplate.opsForSet().add(key,val);
}catch (Exception e){
e.printStackTrace();
logger.error("新增set集合 {} 失败",key);
}
return size;
}
public long removeSet(String key,Object... val){
long size = 0;
try {
size = redisTemplate.opsForSet().remove(key,val);
}catch (Exception e){
e.printStackTrace();
logger.error("移除set集合中{}失败",key);
}
return size;
}
public long countSet(String key){
long size = 0;
try {
size = redisTemplate.opsForSet().size(key);
}catch (Exception e){
e.printStackTrace();
logger.error("计算集合{}数量失败",key);
}
return size;
}
public Boolean hasMemberSet(String key,Object val){
Boolean exist = false;
try {
exist = redisTemplate.opsForSet().isMember(key, val);
} catch (Exception e) {
logger.error("[RedisUtils.hasNumberSet] [error]", e);
}
return exist;
}
public Object rangMember(String key){
return redisTemplate.opsForSet().randomMember(key);
}
public List randomMembers(String key,long nums){
return redisTemplate.opsForSet().randomMembers(key,nums);
}
public Set distinctRandomMembers(String key,Long nums){
return redisTemplate.opsForSet().distinctRandomMembers(key,nums);
}
public Object popMember(String key){
return redisTemplate.opsForSet().pop(key);
}
public List popMember(String key,long nums){
return redisTemplate.opsForSet().pop(key, nums);
}
public Set<Object> members(String key){
return redisTemplate.opsForSet().members(key);
}
public Boolean moveMember(String key1,Object val,String key2){
return redisTemplate.opsForSet().move(key1,val,key2);
}
public Set<Object>intersectMember(String key1,String key2){
return redisTemplate.opsForSet().intersect(key1,key2);
}
public Set<Object> multiIntersectMember(String key, Set<String> key2) {
return redisTemplate.opsForSet().intersect(key, key2);
}
public long storeIntersectMember(String key1,String key2,String key3){
return redisTemplate.opsForSet().intersectAndStore(key1,key2,key3);
}
public Set<Object> unionMember(String key1,String key2){
return redisTemplate.opsForSet().union(key1,key2);
}
public Set<Object> multiUnionMember(String key1, Set<String> keys) {
return redisTemplate.opsForSet().union(key1, keys);
}
public long storeMultiUnionMember(String key1,String key2,String key3){
return redisTemplate.opsForSet().unionAndStore(key1,key2,key3);
}
public Set<Object> difference(String key1,String key2){
return redisTemplate.opsForSet().difference(key1,key2);
}
public Set<Object> difference(String key,Set<String> keys){
return redisTemplate.opsForSet().difference(key,keys);
}
public long differenceAndStore(String key1,String key2,String key3){
return redisTemplate.opsForSet().differenceAndStore(key1,key2,key3);
}
}
package common.redis.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import java.util.Collection;
import java.util.Set;
public class ZsetJedisUtil {
@Autowired
private RedisTemplate redisTemplate;
public Boolean addZset(String key,Object val,double score){
return redisTemplate.opsForZSet().add(key,val,score);
}
public Long addZset(String key, Set<ZSetOperations.TypedTuple<Object>> tuples){
return redisTemplate.opsForZSet().add(key,tuples);
}
public long zsetCount(String key){
return redisTemplate.opsForZSet().size(key);
}
public Set<Object> rangZset(String key,Long start,Long end){
return redisTemplate.opsForZSet().range(key,start,end);
}
public Long removeZset(String key, Object... val){
return redisTemplate.opsForZSet().remove(key,val);
}
public long remove(String key,long start,long end){ return redisTemplate.opsForZSet().removeRange(key,start,end);}
public Double zsetScore(String key,Object val){
return redisTemplate.opsForZSet().score(key,val);
}
public Set<Object> rangeByScore(String key,Double val1,Double val2){
return redisTemplate.opsForZSet().rangeByScore(key,val1,val2);
}
public Set<Object> rangeByScore(String key,Double val1,Double val2,long start,long end){
return redisTemplate.opsForZSet().rangeByScore(key,val1,val2,start,end);
}
public Double addScore(String key,Object val,Double score){
return redisTemplate.opsForZSet().incrementScore(key,val,score);
}
public long rank(String key,Object val){
return redisTemplate.opsForZSet().rank(key,val);
}
public long remove(String key,Object val){
return redisTemplate.opsForZSet().remove(key,val);
}
public Long reverseRank(String key, Object val){
return redisTemplate.opsForZSet().reverseRank(key,val);
}
public Set reverseRangeWithScores(String key, Long start, long end){
return redisTemplate.opsForZSet().reverseRangeWithScores(key,start,end);
}
public Set reverseRangeWithScores(String key,Double val,Double val2,long start,Long end){
return redisTemplate.opsForZSet().reverseRangeByScore(key,val,val2,start,end);
}
public long removeRangeByScore(String key,Double val,Double val2){
return redisTemplate.opsForZSet().removeRangeByScore(key,val,val2);
}
public long remove(String key,Double val,Double val2){
return redisTemplate.opsForZSet().count(key,val,val2);
}
public long unionAndStore(String key1,String key2,String key3){
return redisTemplate.opsForZSet().unionAndStore(key1,key2,key3);
}
public long unionAndStore(String key,Collection collection,String newKey){
return redisTemplate.opsForZSet().unionAndStore(key,collection,newKey);
}
public long intersectAndStore(String key,String key1,String key2){
return redisTemplate.opsForZSet().intersectAndStore(key,key1,key2);
}
public long intersectAndStore(String key,Collection collection,String newKey){
return redisTemplate.opsForZSet().intersectAndStore(key,collection,newKey);
}
}
package common.redis.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.*;
public class HashJedisUtil {
private Logger logger = LoggerFactory.getLogger(HashJedisUtil.class);
@Autowired
private RedisTemplate<String,Object> redisTemplate;
public HashJedisUtil(RedisTemplate<String,Object> redisTemplate){
this.redisTemplate = redisTemplate;
}
public void putAll(String key,Map map){
redisTemplate.opsForHash().putAll(key,map);
}
public long hashSize(String key){
return redisTemplate.opsForHash().size(key);
}
public List<Object> getHashVal(String key){
return redisTemplate.opsForHash().values(key);
}
public Set<Object> getHashKeys(String key){
return redisTemplate.opsForHash().keys(key);
}
public Object getHashKey(String mapKey,String key){
return redisTemplate.opsForHash().get(mapKey,key);
}
public Map<Object, Object> resultMapList(String key){return redisTemplate.opsForHash().entries(key);}
public Boolean hashKey(String key,String hashKey){
return redisTemplate.opsForHash().hasKey(key,hashKey);
}
public void hashPut(String key,String hashKey,Object val){
redisTemplate.opsForHash().put(key,hashKey,val);
}
public Long getHashLen(String key){
return redisTemplate.opsForHash().size(key);
}
public Double increment(String key,String hashKey,Double nums){
return redisTemplate.opsForHash().increment(key, hashKey, nums);
}
public long increment(String key,String hashKey,Long nums){
return redisTemplate.opsForHash().increment(key,hashKey,nums);
}
public List mu(String key,Collection collection){
return redisTemplate.opsForHash().multiGet(key,collection);
}
public Boolean putIfAbsent(String key,String hashKey,Object val){
return redisTemplate.opsForHash().putIfAbsent(key,hashKey,val);
}
public long hashDel(String key,Object... hashKeys){
return redisTemplate.opsForHash().delete(key,hashKeys);
}
}
package common.redis.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.BitFieldSubCommands;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.List;
public class BitMapJedisUtil {
@Autowired
private RedisTemplate redisTemplate;
public Boolean setBitMap(String key,long val,Boolean status){
return redisTemplate.opsForValue().setBit(key,val,status);
}
public Boolean getBitMap(String key, Long val){
return redisTemplate.opsForValue().getBit(key,val);
}
public int countBitMap(String key){
return (int)redisTemplate.execute((RedisCallback<Long>)con -> con.bitCount(key.getBytes()));
}
public List<Long> bitFiled(String key,int start,int end){
return (List<Long>) redisTemplate.execute((RedisCallback<List<Long>>)
con -> con.bitField(key.getBytes(), BitFieldSubCommands.create().get(BitFieldSubCommands.BitFieldType.signed(start))
.valueAt(end)));
}
public Boolean addHyperLogLog(String key,Object val){
return redisTemplate.opsForHyperLogLog().add(key,val)>0;
}
public Boolean addHyperLogLog(String key,Object... val){
return redisTemplate.opsForHyperLogLog().add(key,val)>0;
}
public Long hyperLogCounts(String key){
return redisTemplate.opsForHyperLogLog().size(key);
}
public Boolean mergeHyperLog(String newKey,String... keys){
return redisTemplate.opsForHyperLogLog().union(newKey,keys)>0;
}
public Long addGeo(String key, Point point, String city){
return redisTemplate.opsForGeo().add(key,point,city);
}
public List<Point> points(String key,String... city){
return redisTemplate.opsForGeo().position(key,city);
}
public Distance distanceKilometers(String key, String city, String city1, RedisGeoCommands.DistanceUnit unit){
Distance distance = redisTemplate.opsForGeo()
.distance(key,city,city1,unit);
return distance;
}
public GeoResults<RedisGeoCommands.GeoLocation<String>> rangeOfInformation(String key,double lon,double lat,int scope,Metrics metrics,int limit){
Circle circle = new Circle(new Point(lon, lat), new Distance(scope,metrics));
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(limit);
GeoResults<RedisGeoCommands.GeoLocation<String>> results = redisTemplate.opsForGeo()
.radius(key,circle,args);
return results;
}
public GeoResults<RedisGeoCommands.GeoLocation<String>> cityScope(String key,String city,int scope,Metrics metrics,int limit){
Distance distance = new Distance(scope,metrics);
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(limit);
GeoResults<RedisGeoCommands.GeoLocation<String>> results = redisTemplate.opsForGeo() .radius(key,city,distance,args);
return results;
}
public boolean removeGeo(String key, String city){
return redisTemplate.opsForGeo().remove(key,city) > 0;
}
public Boolean removeGeo(String key,String... city){
return redisTemplate.opsForGeo().remove(key,city) > 0;
}
}