0%

REDIS–另一种写法REDISTEMPLATE

参考链接:https://www.cnblogs.com/jifeng/p/4676863.html

放进去一个key_values,但是实际中作用不大
下面是一个set例子:这个模板和jedis是一样的。如果有不明白的,可以看菜鸟教程的命令
测试类放入工具包
接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
public interface RedisService {
//通过key删除
public long del_set(String keys,byte[] values);
//添加key value 并且设置存活时间(byte)
public void set_set(byte[] key, byte[] value, long liveTime);
//获取集合的长度
public long dbSize();
//清空redis 所有数据
public abstract String flushDB();

public Set<byte[]> getList_set();

}

实现类:

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
@Service(value = “redisService”)
public class RedisServiceImpl implements RedisService{
private static String redisCode = “utf-8”;

@Autowired
private RedisTemplate redisTemplate;
//删除方法
public long del_set(final String keys,final byte[] values) {
System.out.println(“del”);
return (Long) redisTemplate.execute(new RedisCallback() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
Long result = connection.sRem(“user”.getBytes(), values);
return result;
}
});
}
public void set_set(final byte[] key, final byte[] value, final long liveTime) {
System.out.println(“set”+key+” “+value);
redisTemplate.execute(new RedisCallback<Boolean>(){
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
//集合
connection.sAdd(key, value);
System.out.println(“设置成redis成功”);
return true;
}
});
}
/**
* @return
*/
public long dbSize() {
System.out.println(“dbSize”);
return (Long) redisTemplate.execute(new RedisCallback() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
return connection.sCard(“user”.getBytes());
}
});
}
/**
* @return 清空里面的记录
*/

public String flushDB() {
System.out.println(“flushDB”);
return (String) redisTemplate.execute(new RedisCallback() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
connection.flushDb();
return “ok”;
}
});
}

public Set<byte[]> getList_set() {
//final List<User> list=new ArrayList<User>();
return (Set<byte[]>) redisTemplate.execute(new RedisCallback<Set<byte[]>>() {
public Set<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
String key=“user”;
Set<byte[]> bytes=connection.sMembers(key.getBytes());
return bytes;

}
});

}
}

测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 ApplicationContext app = new ClassPathXmlApplicationContext(“ApplicationContext.xml”);
RedisService redisService = (RedisService) app.getBean(“redisService”);
//这个是用于把set的集合变成对象
@Test
public void test2() {
try {
Set<byte[]> userList=redisService.getList_set();
List<User> list=new ArrayList<User>();
for(byte[] bytes:userList) {
User user=(User) SerializeUtils.deSerialize(bytes);
list.add(user);
}
for(User user:list) {
System.out.println(user.getId());
}
System.out.println(redisService.dbSize());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

xml配置文件:

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
<?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:aop=“http://www.springframework.org/schema/aop”;
xmlns:context=“http://www.springframework.org/schema/context”;
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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd “>

<context:component-scan base-package=“com.dh.service”>
<context:exclude-filter type=“annotation”
expression=“org.springframework.stereotype.Controller” />
</context:component-scan>
<bean id=“connectionFactory” class=“org.springframework.data.redis.connection.jedis.JedisConnectionFactory”>
<property name=“hostName” value=“127.0.0.1” />
<property name=“port” value=“6379” />
<!– <property name=”password” value=”${redis.password}” /> –>
</bean>

<bean id=“redisTemplate” class=“org.springframework.data.redis.core.RedisTemplate” >
<property name=“connectionFactory”>
<ref bean=“connectionFactory”/>
</property>
</bean>