| | |
| | | import java.util.Set; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * Redis服务类 |
| | | * @author WWW |
| | | */ |
| | | @Service("redisService") |
| | | @SuppressWarnings("ALL") |
| | | public class RedisService { |
| | | @Autowired |
| | | private RedisTemplate<String, Object> redisTemplate; |
| | | |
| | | /** |
| | | * 获取Redis模板 |
| | | * |
| | | * @return |
| | | */ |
| | | public RedisTemplate<String, Object> getRedisTemplate() { |
| | | return redisTemplate; |
| | | } |
| | | |
| | | /** |
| | | * 设置值到redis中 |
| | | * |
| | | * @param key 键 |
| | | * @param value 值 |
| | | */ |
| | | |
| | | public void put(String key, Object value) { |
| | | redisTemplate.opsForValue().set(key, value); |
| | | } |
| | | |
| | | /** |
| | | * 设置值到redis中,并设置过期时间 |
| | | * |
| | | * @param key 键 |
| | | * @param value 值 |
| | | * @param timeout 时间 |
| | | * @param unit 单位 |
| | | */ |
| | | public void put(String key, Object value, long timeout, TimeUnit unit) { |
| | | redisTemplate.opsForValue().set(key, value, timeout, unit); |
| | | } |
| | | |
| | | /** |
| | | * 根据key获取value |
| | | * |
| | | * @param key 键 |
| | | * @return |
| | | */ |
| | | public Object get(String key) { |
| | | return redisTemplate.opsForValue().get(key); |
| | | } |
| | | |
| | | /** |
| | | * 是否存在key |
| | | * |
| | | * @param key 键 |
| | | * @return |
| | | */ |
| | | public boolean hasKey(String key) { |
| | | return redisTemplate.hasKey(key); |
| | | } |
| | | |
| | | /** |
| | | * 移除key |
| | | * |
| | | * @param key 键 |
| | | */ |
| | | public void delete(String key) { |
| | | if (hasKey(key)) { |
| | | redisTemplate.delete(key); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 清空指定键前缀 |
| | | * |
| | | * @param subKeyString 键前缀 |
| | | */ |
| | | public void clearKeys(String subKeyString) { |
| | | Set<String> keys = redisTemplate.keys(subKeyString + "*"); |
| | | if (null != keys && keys.size() > 0) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 清空所有 |
| | | */ |
| | | public void clearAll() { |
| | | Set<String> keys = redisTemplate.keys("*"); |
| | | if (null != keys && keys.size() > 0) { |