使用Caffeine实现帖子的缓存来优化网站的运行速度

导入依赖

xml 复制代码
		<!-- https://mvnrepository.com/artifact/com.github.ben-manes.caffeine/caffeine -->
		<dependency>
			<groupId>com.github.ben-manes.caffeine</groupId>
			<artifactId>caffeine</artifactId>
			<version>3.1.7</version>
		</dependency>

在Service层中调用Caffeine的接口

java 复制代码
@Service
public class DiscussPostService {

    private final static Logger logger = LoggerFactory.getLogger(DiscussPostService.class);

    @Autowired
    private DiscussPostMapper discussPostMapper;

    @Value("${caffeine.posts.max-size}")
    private int maxSize;

    @Value("${caffeine.posts.expire-seconds}")
    private int expireSeconds;

    // Caffeine's core API: Cache, LoadingCache, AsyncLoadingCache

    // posts list cache
    private LoadingCache<String, List<DiscussPost>> postListCache;

    // posts total number cache
    private LoadingCache<Integer, Integer> postRowsCache;

    @PostConstruct
    public void init(){
        // initialize the post list cache
        postListCache = Caffeine.newBuilder()
                .maximumSize(maxSize)
                .expireAfterWrite(expireSeconds, TimeUnit.SECONDS)
                .build(new CacheLoader<String, List<DiscussPost>>() {
                    @Override
                    public @Nullable List<DiscussPost> load(String key) throws Exception {
                        if(key == null || key.length() == 0){
                            throw new IllegalArgumentException("parameter error: key must not be null");
                        }
                        String[] params = key.split(":");
                        if(params == null || params.length != 2) {
                            throw new IllegalArgumentException("parameter error");
                        }
                        int offset = Integer.valueOf(params[0]);
                        int limit = Integer.valueOf(params[1]);

                        // in there, we can add second level cache, such as Redis
                        // if we can't find data in the Redis, then query it in MySQL

                        logger.debug("load post list from DB...");
                        return discussPostMapper.selectDiscussPosts(0, offset, limit, 1);
                    }
                });
        // initialize the post total number cache
        postRowsCache = Caffeine.newBuilder()
                .maximumSize(maxSize)
                .expireAfterWrite(expireSeconds, TimeUnit.SECONDS)
                .build(new CacheLoader<Integer, Integer>() {
                    @Override
                    public @Nullable Integer load(Integer key) throws Exception {
                        logger.debug("load post list from DB...");
                        return discussPostMapper.selectDiscussPostRows(key);
                    }
                });
    }

    public List<DiscussPost> findDiscussPosts(int userId, int offset, int limit, int orderMode) {
        if(userId == 0 && orderMode == 1){
            return postListCache.get(offset + ":" + limit);
        }
        logger.debug("load post list from DB...");
        return discussPostMapper.selectDiscussPosts(userId, offset, limit, orderMode);
    }

    public int findDiscussPostRows(int userId) {
        if (userId == 0) {
            return postRowsCache.get(userId);
        }
        logger.debug("load post rows from DB...");
        return discussPostMapper.selectDiscussPostRows(userId);
    }
}

测试

java 复制代码
@SpringBootTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = MyCommunityApplication.class)
public class CaffeineTest {

    @Autowired
    private DiscussPostService postService;

    @Test
    public void testCache(){
        System.out.println(postService.findDiscussPosts(0, 0, 10, 1));
        System.out.println(postService.findDiscussPosts(0, 0, 10, 1));
        System.out.println(postService.findDiscussPosts(0, 0, 10, 1));
        System.out.println(postService.findDiscussPosts(0, 0, 10, 0));
    }
}

结果:

可以看到,第一次记录还未加入缓存,所以是从DB中加载,而后两次访问记录都是从Caffeine中加载的;最后一次访问是强制要求从DB中访问的。

通过压力测试检测使用缓存提高的效率

相关推荐
vvilkim12 分钟前
Redis 事务与管道:原理、区别与应用实践
数据库·redis·缓存
济南java开发,求内推32 分钟前
记一次缓存填坑省市区级联获取的操作
缓存
追风赶月、1 小时前
【Redis】redis用作缓存和分布式锁
redis·分布式·缓存
vvilkim2 小时前
Redis 发布订阅模式深度解析:原理、应用与实践
数据库·redis·缓存
呦呦鹿鸣Rzh11 小时前
缓存的相关内容
缓存
dddaidai12311 小时前
Redis解析
数据库·redis·缓存
Chasing__Dreams15 小时前
Redis--基础知识点--26--过期删除策略 与 淘汰策略
数据库·redis·缓存
源远流长jerry15 小时前
MySQL的缓存策略
数据库·mysql·缓存
hudawei99620 小时前
flutter缓存网络视频到本地,可离线观看
flutter·缓存·音视频
小哈里20 小时前
【pypi镜像源】使用devpi实现python镜像源代理(缓存加速,私有仓库,版本控制)
开发语言·python·缓存·镜像源·pypi