1.redis工具类
@Component
@SuppressWarnings({"unchecked", "rawtypes"})
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
public <K> long getExpireTime(K key) {
Long expire = redisTemplate.getExpire(key, TimeUnit.SECONDS);
if (expire != null) {
return expire;
}
return -2;
}
public <K> void setExpireTime(K key, long expireTime) {
try {
if (expireTime > 0) {
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public <K> void removeExpireTime(K key) {
redisTemplate.boundValueOps(key).persist();
}
public <K> Set<K> keys(K key) {
return redisTemplate.keys(key);
}
public <K> boolean hasKey(K key) {
try {
return Boolean.TRUE.equals(redisTemplate.hasKey(key));
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public <K> void delete(Collection<K> keys) {
redisTemplate.delete(keys);
}
public <K, V> Boolean setNx(K key, V value, long expire) {
return redisTemplate.opsForValue().setIfAbsent(key, value, expire, TimeUnit.SECONDS);
}
public <K, V> Boolean setNx(K key, V value, long expire, long timeout) {
long start = System.currentTimeMillis();
for (; ; ) {
if (Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(key, value, expire, TimeUnit.SECONDS))) {
return true;
}
if (System.currentTimeMillis() - start > timeout) {
return false;
}
}
}
public <K, V> boolean releaseNx(K key, V value) {
Object currentValue = redisTemplate.opsForValue().get(key);
if (String.valueOf(currentValue) != null && value.equals(currentValue)) {
return Boolean.TRUE.equals(redisTemplate.opsForValue().getOperations().delete(key));
}
return false;
}
public <K, V> void set(K key, V value) {
try {
redisTemplate.opsForValue().set(key, value);
} catch (Exception e) {
e.printStackTrace();
}
}
public <K, V> void set(K key, V value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
redisTemplate.opsForValue().set(key, value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Long incrBy(String key, long number) {
return (Long) redisTemplate.execute((RedisCallback<Object>) connection -> connection.incrBy(key.getBytes(), number));
}
public Long decrBy(String key, long number) {
return (Long) redisTemplate.execute((RedisCallback<Object>) connection -> connection.decrBy(key.getBytes(), number));
}
public <K, V> V get(K key) {
BoundValueOperations<K, V> boundValueOperations = redisTemplate.boundValueOps(key);
return boundValueOperations.get();
}
public <K, V> void listRightPush(K key, V value) {
ListOperations<K, V> listOperations = redisTemplate.opsForList();
listOperations.rightPush(key, value);
}
public <K, V> void listLeftPush(K key, V value) {
ListOperations<K, V> listOperations = redisTemplate.opsForList();
listOperations.leftPush(key, value);
}
public <K, V> void listRightPushAll(K key, List<V> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
} catch (Exception e) {
e.printStackTrace();
}
}
public <K, V> void listLeftPushAll(K key, List<V> value) {
try {
redisTemplate.opsForList().leftPushAll(key, value);
} catch (Exception e) {
e.printStackTrace();
}
}
public <K, V> V listGetWithIndex(K key, long index) {
ListOperations<K, V> listOperations = redisTemplate.opsForList();
return listOperations.index(key, index);
}
public <K, V> V listLeftPop(K key) {
ListOperations<K, V> listOperations = redisTemplate.opsForList();
return listOperations.leftPop(key);
}
public <K, V> V listLeftPop(K key, long timeout, TimeUnit unit) {
ListOperations<K, V> listOperations = redisTemplate.opsForList();
return listOperations.leftPop(key, timeout, unit);
}
public <K, V> V listRightPop(K key) {
ListOperations<K, V> listOperations = redisTemplate.opsForList();
return listOperations.rightPop(key);
}
public <K, V> V listRightPop(K key, long timeout, TimeUnit unit) {
ListOperations<K, V> listOperations = redisTemplate.opsForList();
return listOperations.leftPop(key, timeout, unit);
}
public <K, V> List<V> listRange(K key, long start, long end) {
try {
ListOperations<K, V> listOperations = redisTemplate.opsForList();
return listOperations.range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public <K> long listSize(K key) {
return redisTemplate.opsForList().size(key);
}
public <K, V> void listSet(K key, long index, V value) {
try {
redisTemplate.opsForList().set(key, index, value);
} catch (Exception e) {
e.printStackTrace();
}
}
public <K, V> long listRemove(K key, long count, V value) {
Long count1 = redisTemplate.opsForList().remove(key, count, value);
if (count1 != null) {
return count1;
}
return 0;
}
public <K, HK, HV> HV hashGet(K key, String item) {
HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
return hashOperations.get(key, item);
}
public <K, HK, HV> Map<HK, HV> hashMGet(K key) {
HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
return hashOperations.entries(key);
}
public <K> void hashMSet(K key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
} catch (Exception e) {
e.printStackTrace();
}
}
public <K> void hashMSet(K key, Map<String, Object> map, long expireTime) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (expireTime > 0) {
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public <K, HK, HV> void hashPut(K key, HK hKey, HV value) {
try {
HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
hashOperations.put(key, hKey, value);
} catch (Exception e) {
e.printStackTrace();
}
}
public <K, HK, HV> void hashPut(K key, HK hKey, HV value, long expireTime) {
try {
redisTemplate.opsForHash().put(key, hKey, value);
if (expireTime > 0) {
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public <K, HK, HV> boolean hashHasKey(K key, HK hKey) {
HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
return hashOperations.hasKey(key, hKey);
}
public <K, HK, HV> List<HV> hashValues(K key) {
HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
return hashOperations.values(key);
}
public <K, HK, HV> Set<HK> hashHKeys(K key) {
HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
return hashOperations.keys(key);
}
public <K, HK, HV> Long hashDelete(K key, Object... hashKeys) {
HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
return hashOperations.delete(key, hashKeys);
}
public <K, V> void setAdd(K key, V... values) {
redisTemplate.opsForSet().add(key, values);
}
public <K, V> void setAdd(K key, long expireTime, V... values) {
redisTemplate.opsForSet().add(key, values);
if (expireTime > 0) {
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
}
}
public <K> long setSize(K key) {
Long size = redisTemplate.opsForSet().size(key);
if (size != null) {
return size;
}
return 0;
}
public <K, V> Set<V> setValues(K key) {
SetOperations<K, V> setOperations = redisTemplate.opsForSet();
return setOperations.members(key);
}
public <K, V> boolean setHasKey(K key, V value) {
return Boolean.TRUE.equals(redisTemplate.opsForSet().isMember(key, value));
}
public <K, V> Long setDelete(K key, Object... value) {
SetOperations<K, V> setOperations = redisTemplate.opsForSet();
return setOperations.remove(key, value);
}
public <K, V> void zSetAdd(K key, V value, long score) {
ZSetOperations<K, V> zSetOperations = redisTemplate.opsForZSet();
zSetOperations.add(key, value, score);
}
public <K, V> Set<V> zSetValuesRange(K key, long score1, long score2) {
ZSetOperations<K, V> zSetOperations = redisTemplate.opsForZSet();
return zSetOperations.range(key, score1, score2);
}
public <K, V> Long zSetDeleteByValue(K key, Object... value) {
ZSetOperations<K, V> zSetOperations = redisTemplate.opsForZSet();
return zSetOperations.remove(key, value);
}
public <K, V> Long zSetDeleteRange(K key, long size1, long size2) {
ZSetOperations<K, V> zSetOperations = redisTemplate.opsForZSet();
return zSetOperations.removeRange(key, size1, size2);
}
public <K, V> Long zSetDeleteByScore(K key, long score1, long score2) {
ZSetOperations<K, V> zSetOperations = redisTemplate.opsForZSet();
return zSetOperations.removeRangeByScore(key, score1, score2);
}
}
2.使用
@Override
public List<User> findAll() {
List<User> userList = (List<User>) redisUtils.get("userlist");
if(userList==null){
userList=userMapper.selectAll();
redisUtils.set("userlist",userList);
}else{
System.out.println("redis...");
}
return userMapper.selectAll();
}