Redis中SortedSet一些简单的API操作(JAVA)
接口
public interface IRedisService {
public boolean zadd(String key, String value, double score);
public long zadd(String key, Set<ZSetOperations.TypedTuple<String>> tupleSet);
public long zcard(String key);
public long zcount(String key, double minScore, double maxScore);
public double zincrby(String key, String value, double incrScore);
public Set<String> zrange(String key, long startIndex, long endIndex);
public Set<ZSetOperations.TypedTuple<String>> zrangeWithScores(String key, long startIndex, long endIndex);
public Set<String> zrangeByScore(String key, double startSocre, double endScore);
public Set<String> zrangeByScore(String key, double startSocre, double endScore, long offset, long count);
public Set<ZSetOperations.TypedTuple<String>> zrangeWithScores(String key, double startSocre, double endScore);
public Set<ZSetOperations.TypedTuple<String>> zrangeWithScores(String key, double startSocre, double endScore, long offset, long count);
public Long zrank(String key, String value);
public long zremove(String key, String... value);
public long zremoveRangeByScore(String key, double startSocre, double endScore);
public Double zscore(String key, String value);
}
2.实现
public class RedisServiceImpl implements IRedisService {
protected static Logger logger = LoggerFactory.getLogger(RedisServiceImpl.class);
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public boolean zadd(String key, String value, double score) {
boolean result = true;
try {
stringRedisTemplate.opsForZSet().add(key, value, score);
} catch (Exception e) {
logger.warn("zadd {}", key, e);
result = false;
}
return result;
}
@Override
public long zadd(String key, Set<ZSetOperations.TypedTuple<String>> tupleSet) {
long result = 0;
try {
result = stringRedisTemplate.opsForZSet().add(key, tupleSet);
} catch (Exception e) {
logger.warn("zadd {}", key, e);
}
return result;
}
@Override
public long zcard(String key) {
long result = 0;
try {
result = stringRedisTemplate.opsForZSet().zCard(key);
} catch (Exception e) {
logger.warn("zcard {}", key, e);
}
return result;
}
@Override
public long zcount(String key, double minScore, double maxScore) {
long result = 0;
try {
result = stringRedisTemplate.opsForZSet().count(key, minScore, maxScore);
} catch (Exception e) {
logger.warn("zcount {}", key, e);
}
return result;
}
@Override
public double zincrby(String key, String value, double incrScore) {
double result = 0.00;
try {
result = stringRedisTemplate.opsForZSet().incrementScore(key, value, incrScore);
} catch (Exception e) {
logger.warn("zincrby {}", key, e);
}
return result;
}
@Override
public Set<String> zrange(String key, long startIndex, long endIndex) {
Set<String> set = null;
try {
set = stringRedisTemplate.opsForZSet().range(key, startIndex, endIndex);
} catch (Exception e) {
logger.warn("zincrby {}", key, e);
}
return set;
}
@Override
public Set<ZSetOperations.TypedTuple<String>> zrangeWithScores(String key, long startIndex, long endIndex) {
Set<ZSetOperations.TypedTuple<String>> set = null;
try {
set = stringRedisTemplate.opsForZSet().rangeWithScores(key, startIndex, endIndex);
} catch (Exception e) {
logger.warn("zrangeWithScores {}", key, e);
}
return set;
}
@Override
public Set<String> zrangeByScore(String key, double startSocre, double endScore) {
Set<String> set = null;
try {
set = stringRedisTemplate.opsForZSet().rangeByScore(key, startSocre, endScore);
} catch (Exception e) {
logger.warn("zrangeByScore {}", key, e);
}
return set;
}
@Override
public Set<String> zrangeByScore(String key, double startSocre, double endScore, long offset, long count) {
Set<String> set = null;
try {
set = stringRedisTemplate.opsForZSet().rangeByScore(key, startSocre, endScore, offset, count);
} catch (Exception e) {
logger.warn("zrangeByScore {}", key, e);
}
return set;
}
@Override
public Set<ZSetOperations.TypedTuple<String>> zrangeWithScores(String key, double startSocre, double endScore) {
Set<ZSetOperations.TypedTuple<String>> set = null;
try {
set = stringRedisTemplate.opsForZSet().rangeByScoreWithScores(key, startSocre, endScore);
} catch (Exception e) {
logger.warn("zrangeWithScores {}", key, e);
}
return set;
}
@Override
public Set<ZSetOperations.TypedTuple<String>> zrangeWithScores(String key, double startSocre, double endScore, long offset, long count) {
Set<ZSetOperations.TypedTuple<String>> set = null;
try {
set = stringRedisTemplate.opsForZSet().rangeByScoreWithScores(key, startSocre, endScore, offset, count);
} catch (Exception e) {
logger.warn("zrangeWithScores {}", key, e);
}
return set;
}
@Override
public Long zrank(String key, String value) {
Long result = null;
try {
result = stringRedisTemplate.opsForZSet().rank(key, value);
} catch (Exception e) {
logger.warn("zrank {}", key, e);
}
return result;
}
@Override
public long zremove(String key, String... value) {
long result = 0;
try {
result = stringRedisTemplate.opsForZSet().remove(key, value);
} catch (Exception e) {
logger.warn("zremove {}", key, e);
}
return result;
}
@Override
public long zremoveRangeByScore(String key, double startSocre, double endScore) {
long result = 0;
try {
result = stringRedisTemplate.opsForZSet().removeRangeByScore(key, startSocre, endScore);
} catch (Exception e) {
logger.warn("zremoveRangeByScore {}", key, e);
}
return result;
}
@Override
public Double zscore(String key, String value) {
Double result = null;
try {
result = stringRedisTemplate.opsForZSet().score(key, value);
} catch (Exception e) {
logger.warn("zscore {}", key, e);
}
return result;
}
}