1. 引入相关依赖
在pom.xml中添加以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--springboot2.X默认使用lettuce连接池,需要引入commons-pool2-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2. 在ArticleServerImpl中加入RedisTemplate
import com.nevergetme.nevergetmeweb.bean.Article;
import com.nevergetme.nevergetmeweb.mapper.ArticleMapper;
import com.nevergetme.nevergetmeweb.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Service
public class ArticleServerImpl implements ArticleService {
private final String ARTICLE_KEY = "article_";
private final String ARTICLE_LIST_KEY = "article_list";
@Autowired
private ArticleMapper articleMapper;
@Autowired
private RedisTemplate redisTemplate;
@Override
public List<Article> findAllArticle() {
List<Article> articles = articleMapper.getAllArticle();
return articles;
}
@Override
public List<Article> findUserArticle(int userid) {
return articleMapper.getUserArticle(userid);
}
/**
* 如果缓存中存在,则从缓存中读取
*
* @param id
* @return
*/
@Override
public Article getArticleById(int id) {
String key = ARTICLE_KEY + id;
ValueOperations<String, Article> operations = redisTemplate.opsForValue();
if (redisTemplate.hasKey(key)) {
return operations.get(key);
} else {
Article article = articleMapper.getArticleById(id);
operations.set(key, article, 5, TimeUnit.HOURS);
return article;
}
}
@Override
public int createNewArticle(Article article) {
ValueOperations<String, Article> operations = redisTemplate.opsForValue();
int articleID = articleMapper.createNewArticle(article);
if (articleID != 0) {
String key = ARTICLE_KEY + articleID;
if (redisTemplate.hasKey(key)) {
redisTemplate.delete(key);
}
Article articleNew = getArticleById(articleID);
if (articleNew != null) {
operations.set(key, articleNew, 5, TimeUnit.HOURS);
}
//updateArticleListInRedis();
}
return articleID;
}
private void updateArticleListInRedis() {
ListOperations<String, Article> operations = redisTemplate.opsForList();
if (redisTemplate.hasKey(ARTICLE_LIST_KEY)) {
operations.leftPop(ARTICLE_LIST_KEY);
}
List<Article> articleList = articleMapper.getArticleList();
if (articleList != null && articleList.size() > 0)
operations.rightPushAll(ARTICLE_LIST_KEY, articleList);
}
/**
* 获取文章列表
* 首先从缓存中读取,如果缓存中没有,则需要读取数据库,然后更新缓存再返回
*
* @return
*/
@Override
public List<Article> getArticleList() {
return articleMapper.getArticleList();
// ListOperations<String,Article> operations=redisTemplate.opsForList();
// if(redisTemplate.hasKey(ARTICLE_LIST_KEY)){
// System.out.println("read article list from redis");
// return operations.range(ARTICLE_LIST_KEY,0,-1);
// }else{
// List<Article> articleList=articleMapper.getArticleList();
// if(articleList!=null&&articleList.size()>0)
// operations.rightPushAll(ARTICLE_LIST_KEY,articleList);
// return articleList;
// }
}
@Override
public void updateVisitTimes(int id) {
articleMapper.updateVisitTimes(id);
}
}
Tips: 这地方我出现了一个问题,在为列表加缓存的时候,出现了如下结果
而实际上我的list是13个,这个缓存中确存储了10个,不知道是什么原因,然后我就把这个注释掉了
3. 在配置文件中加入相关设置
在properties中加入如下配置
## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=127.0.0.1
spring.redis.timeout=60s
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=123456
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.min-idle=10
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=10000
设置密码时,需要修改reids.windows-service.conf中的requirepass
在Linux中同理,同时需要注意bind设置,如果访问不了,就把bind注释掉
然后就可以了,加缓存还是很简单的
项目地址