SSM框架中使用Redis单机版


上一篇文章就说要尝试在代码中实现Redis使用,但是有一段时间很(tou)忙(lan),所以现在才写。

Maven依赖

1
2
3
4
5
6
7
8
9
10
11
12
<!-- Redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>

配置文件

config.properties:

1
2
3
#Redis config
redis.host=localhost
redis.port=6379

spring-jedis.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
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<!-- 连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="30" />
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="10" />
<!-- 每次释放连接的最大数目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 释放连接的扫描间隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 连接最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在获取连接的时候检查有效性, 默认false -->
<property name="testOnBorrow" value="true" />
<!-- 在空闲时检查有效性, 默认false -->
<property name="testWhileIdle" value="true" />
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<!-- jedis客户端单机版 -->
<bean id="redisClient" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="${redis.host}"/>
<constructor-arg name="port" value="${redis.port}"/>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
</bean>

</beans>

配置简单的API

演示简单的使用,所以很多异常状态没有设置。
创建一个接口和接口实现类。
IJedisClientService:

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
package cn.sevenyuan.demo.cache;

/**
* Created by JingQ on 2017/9/15.
*/
public interface IJedisClientService {

String get(String key);

String set(String key, String value);

String hget(String hkey, String key);

boolean exist(String key);

long hset(String hkey, String key, String value);

long incr(String key);

long expire(String key, int second);

long ttl(String key);

long del(String key);

long hdel(String hkey, String key);
}

JedsiClientService:

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cn.sevenyuan.demo.cache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
* Created by JingQ on 2017/9/15.
*/
@Service
public class JedisClientService implements IJedisClientService{

@Autowired
private JedisPool jedisPool;

@Override
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.get(key);
jedis.close();
return string;
}

@Override
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String string = jedis.set(key, value);
jedis.close();
return string;
}

@Override
public String hget(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
String string = jedis.hget(hkey, key);
jedis.close();
return string;
}

@Override
public boolean exist(String key) {
Jedis jedis = jedisPool.getResource();
boolean result = jedis.exists(key);
jedis.close();
return result;
}

@Override
public long hset(String hkey, String key, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(hkey, key, value);
jedis.close();
return result;
}

@Override
public long incr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.incr(key);
jedis.close();
return result;
}

@Override
public long expire(String key, int second) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.expire(key, second);
jedis.close();
return result;
}

@Override
public long ttl(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.ttl(key);
jedis.close();
return result;
}

@Override
public long del(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.del(key);
jedis.close();
return result;
}

@Override
public long hdel(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hdel(hkey, key);
jedis.close();
return result;
}
}

进行调用

demo使用的是mysql作为最终结果保存,redis作为缓存调用,一般的套路是查询到的结果放进redis中,设置过期时间,在数据更新或者删除,将这个key值删掉,重新设置。

例:图书的增删查改

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package cn.sevenyuan.demo.service.impl;

import cn.sevenyuan.demo.cache.IJedisClientService;
import cn.sevenyuan.demo.dao.IBookDao;
import cn.sevenyuan.demo.domain.Book;
import cn.sevenyuan.demo.service.IBookService;
import cn.sevenyuan.demo.utils.RedisKeyUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;

/**
* RedisKeyUtils是自己写的key值生成器,按照个人命名习惯去写
* Created by JingQ on 2017/9/5.
*/
@Service("bookService")
public class BookService implements IBookService {
private final String className = "BookService";
@Autowired
private IBookDao bookDao;

@Autowired
private IJedisClientService jedisClientService;

@Override
public Book selectBookById(Integer id) {
String key = RedisKeyUtils.generateKey(className, "book", String.valueOf(id));
boolean isExist = jedisClientService.exist(key);
Book book = null;
if (isExist){
String result = jedisClientService.get(key);
book = JSONObject.parseObject(result, Book.class);
}else {
book = bookDao.selectById(id);
if (!ObjectUtils.isEmpty(book)){
jedisClientService.set(key, JSONObject.toJSON(book).toString());
}
}
return book;
}

@Override
public int insert(Book book) {
if (ObjectUtils.isEmpty(book)){
return 0;
}
return bookDao.insert(book);
}

@Override
public int deleteByPrimaryKey(Integer id) {
String key = RedisKeyUtils.generateKey(className, "book", String.valueOf(id));
if (jedisClientService.exist(key)){
jedisClientService.del(key);
}
return bookDao.deleteByPrimaryId(id);
}

@Override
public int updateByPrimaryKey(Book book) {
if (ObjectUtils.isEmpty(book)){
return 0;
}
String key = RedisKeyUtils.generateKey(className, "book", String.valueOf(book.getBookId()));
if (jedisClientService.exist(key)){
jedisClientService.del(key);
}
return bookDao.updateByPrimaryId(book);
}

@Override
public List<Book> selectBookByIds(List<Integer> list) {
if (CollectionUtils.isEmpty(list)){
Collections.emptyList();
}
return bookDao.selectByIds(list);
}
}

单机版使用基本就是这个套路了,最近还在看ZooKeeper和简单的jvm,之后也会记录一下学习的内容~