[redis] 普通 RedisPool 的 CRUD 实现

时间:2023-03-09 06:41:17
[redis] 普通 RedisPool 的 CRUD 实现

参考链接:

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)结果

[redis] 普通 RedisPool 的 CRUD 实现