spring整合redis(常用数据类型操作)

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);
    }
}
相关推荐
自身就是太阳24 分钟前
深入理解 Spring 事务管理及其配置
java·开发语言·数据库·spring
Aries2631 小时前
Spring事务传播行为详解
java·数据库·spring
陌上少年,且听这风吟2 小时前
【已解决】SpringBoot3项目整合Druid依赖:Druid监控页面404报错
java·spring boot·spring
骆晨学长3 小时前
基于springboot学生健康管理系统的设计与实现
java·开发语言·spring boot·后端·spring
二十雨辰3 小时前
[苍穹外卖]-09Spring Task定时任务
java·数据库·spring
我是小酒3 小时前
掌握 Spring:从新手到高手的常见问题汇总
java·后端·spring·springboot
捕风捉你3 小时前
Spring 源码解读:手动实现Environment抽象与配置属性
后端·spring
时间会证明一切.4 小时前
【Java面试】第十天
java·开发语言·spring·面试
酷帅且洋仔4 小时前
Redis——常用数据类型hash
数据库·redis
wxin_VXbishe5 小时前
springboot瑜伽课约课小程序-计算机毕业设计源码87936
java·c++·spring boot·python·spring·servlet·php