Redis tool class

Prodigal son Tang Shao 2022-02-13 07:29:31 阅读数:557

redis tool class

1.RedisUtils

@Component
public class RedisUtils
{

@Autowired
public RedisTemplate redisTemplate;
/** * Cache basic objects ,Integer、String、 Entity class, etc * * @param key Cached key value * @param value The value of the cache */
public <T> void setCacheObject(final String key, final T value)
{

redisTemplate.opsForValue().set(key, value);
}
/** * Cache basic objects ,Integer、String、 Entity class, etc * * @param key Cached key value * @param value The value of the cache * @param timeout Time * @param timeUnit The granularity of time */
public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
{

redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
/** * Set the effective time * * @param key Redis key * @param timeout Timeout time * @return true= Set up the success ;false= Setup failed */
public boolean expire(final String key, final long timeout)
{

return expire(key, timeout, TimeUnit.SECONDS);
}
/** * Set the effective time * * @param key Redis key * @param timeout Timeout time * @param unit Time unit * @return true= Set up the success ;false= Setup failed */
public boolean expire(final String key, final long timeout, final TimeUnit unit)
{

return redisTemplate.expire(key, timeout, unit);
}
/** * Get the cached base object . * * @param key Cache key values * @return Cache the data corresponding to the key value */
public <T> T getCacheObject(final String key)
{

ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
}
/** * Delete a single object * * @param key */
public boolean deleteObject(final String key)
{

return redisTemplate.delete(key);
}
/** * Delete collection object * * @param collection Multiple objects * @return */
public long deleteObject(final Collection collection)
{

return redisTemplate.delete(collection);
}
/** * cache List data * * @param key Cached key value * @param dataList To be cached List data * @return Cached objects */
public <T> long setCacheList(final String key, final List<T> dataList)
{

Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
/** * Get cached list object * * @param key Cached key value * @return Cache the data corresponding to the key value */
public <T> List<T> getCacheList(final String key)
{

return redisTemplate.opsForList().range(key, 0, -1);
}
/** * cache Set * * @param key Cache key values * @param dataSet Cached data * @return Objects that cache data */
public <T> long setCacheSet(final String key, final Set<T> dataSet)
{

Long count = redisTemplate.opsForSet().add(key, dataSet);
return count == null ? 0 : count;
}
/** * Get cached set * * @param key * @return */
public <T> Set<T> getCacheSet(final String key)
{

return redisTemplate.opsForSet().members(key);
}
/** * cache Map * * @param key * @param dataMap */
public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
{

if (dataMap != null) {

redisTemplate.opsForHash().putAll(key, dataMap);
}
}
/** * Get cached Map * * @param key * @return */
public <T> Map<String, T> getCacheMap(final String key)
{

return redisTemplate.opsForHash().entries(key);
}
/** * Go to Hash Stored data in * * @param key Redis key * @param hKey Hash key * @param value value */
public <T> void setCacheMapValue(final String key, final String hKey, final T value)
{

redisTemplate.opsForHash().put(key, hKey, value);
}
/** * obtain Hash Data in * * @param key Redis key * @param hKey Hash key * @return Hash Objects in the */
public <T> T getCacheMapValue(final String key, final String hKey)
{

HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey);
}
/** * Get multiple Hash Data in * * @param key Redis key * @param hKeys Hash keyset * @return Hash A collection of objects */
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
{

return redisTemplate.opsForHash().multiGet(key, hKeys);
}
/** * Get the list of cached basic objects * * @param pattern String prefix * @return The object list */
public Collection<String> keys(final String pattern)
{

return redisTemplate.keys(pattern);
}
/** * zSet Stored value * * @param key * @param value * @param score * @return */
public static Boolean zsSet(String key, String value, Long score){

return redisTemplate.opsForZSet().add(key,value,score);
}
/** * according to key and value obtain score * @param key * @return */
public static Double zsGet(String key, String value){

return redisTemplate.opsForZSet().score(key, value);
}
}
copyright:author[Prodigal son Tang Shao],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130729304207.html