[redis] 普通 RedisPool 的 CRUD 实现

本文详细介绍了如何在Maven项目中配置并使用Spring Data Redis存储对象,包括配置RedisTemplate类、操作示例及测试过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

参考链接:

redisTemplate 操作

Maven中Spring-Data-Redis存储对象(redisTemplate)

 

1、配置RedisTempate类

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxTotal}"></property>
        <property name="maxIdle" value="${redis.maxIdle}" /> 
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>
    
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
        p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="poolConfig"/>
    
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
    </bean>        
    
</beans>

 

属性文件

# Redis settings
redis.host=192.168.11.100
redis.port=6379
#redis.pass=hugsh

redis.maxIdle=25
redis.maxTotal=250
#redis.maxActive=600 invalid in2.4
redis.maxWait=1000
redis.testOnBorrow=true

 

2、操作示例

1)创建User类

必须实现或者间接实现Serializable接口:

Redis存储对象是使用序列化,spring-data-redis已经将序列化的功能内置,不需要我们去管,我们只需要调用api就可以使用。SerialVersionUID字段对序列化扩展有用,为了以后扩展或者缩减字段时不会造成反序列化出错。

public class User implements Serializable {

    private static final long serialVersionUID = -7898194272883238670L;

    public static final String OBJECT_KEY = "USER";

    public User() {
    }

    public User(String id) {
    }

    public User(String id, String name) {
        this.id = id;
        this.name = name;
    }

    private String id;

    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return "User [id=" + id + ", name=" + name + "]";
    }

    public String getKey() {
        return getId();
    }

    public String getObjectKey() {
        return OBJECT_KEY;
    }
}

2)创建userService类来操作redis增删查改缓存对象。

public class UserService {

    RedisTemplate<String, User> redisTemplate;

    public RedisTemplate<String, User> getRedisTemplate() {
        return redisTemplate;
    }

    public void setRedisTemplate(RedisTemplate<String, User> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
    
    public void put(User user) {
        redisTemplate.opsForHash().put(user.getObjectKey(), user.getKey(), user);
    }

    public void delete(User key) {
        redisTemplate.opsForHash().delete(key.getObjectKey(), key.getKey());
    }

    public User get(User key) {
        return (User) redisTemplate.opsForHash().get(key.getObjectKey(), key.getKey());
    }
}

3)配置service的bean

<bean id="userService" class="Service.UserService">
    <property name="redisTemplate">
        <ref bean="redisTemplate" />
    </property>
</bean>

4)测试

public class Main {
    public static void main( String[] args )
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:/conf/applicationContext.xml");
        UserService userService =  (UserService) applicationContext.getBean("userService");
            
        User user1 = new User("user1ID", "User 1");
        User user2 = new User("user2ID", "User 2");
        
        System.out.println("==== getting objects from redis ====");
        System.out.println("User is not in redis yet: " + userService.get(user1));
        System.out.println("User is not in redis yet: " + userService.get(user2));
        
        System.out.println("==== putting objects into redis ====");
        userService.put(user1);
        userService.put(user2);
        
        System.out.println("==== getting objects from redis ====");
        System.out.println("User should be in redis yet: " + userService.get(user1));
        System.out.println("User should be in redis yet: " + userService.get(user2));
        
        System.out.println("==== deleting objects from redis ====");
        userService.delete(user1);
        userService.delete(user2);
        
        System.out.println("==== getting objects from redis ====");
        System.out.println("User is not in redis yet: " + userService.get(user1));
        System.out.println("User is not in redis yet: " + userService.get(user2));

    }
}

5)结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值