1、字符串(String)操作
java
复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class RedisStringService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void setStringValue(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}
public String getStringValue(String key) {
return (String) redisTemplate.opsForValue().get(key);
}
}
2、列表 (List) 操作
java
复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class RedisListService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void pushListValue(String key, String value) {
redisTemplate.opsForList().rightPush(key, value); // 右侧推入
}
public String popListValue(String key) {
return (String) redisTemplate.opsForList().leftPop(key); // 左侧弹出
}
public List<Object> getListValues(String key, long start, long end) {
return redisTemplate.opsForList().range(key, start, end); // 获取指定范围的列表元素
}
}
3、集合 (Set) 操作
java
复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class RedisSetService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void addSetValue(String key, String value) {
redisTemplate.opsForSet().add(key, value);
}
public Set<Object> getSetValues(String key) {
return redisTemplate.opsForSet().members(key);
}
public Long removeSetValue(String key, String value) {
return redisTemplate.opsForSet().remove(key, value);
}
}
4、有序集合 (Sorted Set) 操作
java
复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class RedisZSetService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void addZSetValue(String key, String value, double score) {
redisTemplate.opsForZSet().add(key, value, score);
}
public Set<Object> getZSetValues(String key, long start, long end) {
return redisTemplate.opsForZSet().range(key, start, end);
}
public Long removeZSetValue(String key, String value) {
return redisTemplate.opsForZSet().remove(key, value);
}
}
5、哈希 (Hash) 操作
java
复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class RedisHashService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void putHashValue(String key, String hashKey, String value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}
public Object getHashValue(String key, String hashKey) {
return redisTemplate.opsForHash().get(key, hashKey);
}
public Map<Object, Object> getAllHashValues(String key) {
return redisTemplate.opsForHash().entries(key);
}
public Long deleteHashField(String key, String hashKey) {
return redisTemplate.opsForHash().delete(key, hashKey);
}
}