1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| package com.example.service;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service public class RedisService { @Autowired private RedisTemplate<String, Object> redisTemplate; public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public void set(String key, Object value, long timeout, TimeUnit unit) { redisTemplate.opsForValue().set(key, value, timeout, unit); } public Object get(String key) { return redisTemplate.opsForValue().get(key); } public Boolean delete(String key) { return redisTemplate.delete(key); } public Boolean hasKey(String key) { return redisTemplate.hasKey(key); } public Boolean expire(String key, long timeout, TimeUnit unit) { return redisTemplate.expire(key, timeout, unit); } public Long getExpire(String key) { return redisTemplate.getExpire(key); } public Long increment(String key) { return redisTemplate.opsForValue().increment(key); } public Long decrement(String key) { return redisTemplate.opsForValue().decrement(key); } public void hSet(String key, String field, Object value) { redisTemplate.opsForHash().put(key, field, value); } public Object hGet(String key, String field) { return redisTemplate.opsForHash().get(key, field); } public void lPush(String key, Object value) { redisTemplate.opsForList().leftPush(key, value); } public Object lPop(String key) { return redisTemplate.opsForList().leftPop(key); } public Long sAdd(String key, Object... values) { return redisTemplate.opsForSet().add(key, values); } public Boolean zAdd(String key, Object value, double score) { return redisTemplate.opsForZSet().add(key, value, score); } }
|