【SpringBoot】:j2cache入门案例
文章目录
- 前言
- 第一步:创建maven工程j2cache_demo并配置pom.xml文件
- 第二步:创建application.yml
- 第三步:创建/resources/caffeine.properties文件
- 第四步:创建MyController
- 第五步:创建启动类
- 目录结构
前言
j2cache是OSChina目前正在使用的两级缓存框架。
j2cache的两级缓存结构:
- L1: 进程内缓存 caffeine/ehcache
- L2: 集中式缓存 Redis/Memcached
j2cache其实并不是在重复造轮子,而是作资源整合,即将Ehcache、Caffeine、redis、Spring Cache等进行整合。
由于大量的缓存读取会导致L2的网络成为整个系统的瓶颈,因此L1的目标是降低对L2的读取次数。该缓存框架主要用于集群环境中。单机也可使用,用于避免应用重启导致的ehcache缓存数据丢失。
j2cache从1.3.0版本开始支持JGroups和Redis Pub/Sub两种方式进行缓存事件的通知。
数据读取顺序 -> L1 -> L2 -> DB
第一步:创建maven工程j2cache_demo并配置pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/></parent><groupId>org.example</groupId><artifactId>j2cache_demo</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>net.oschina.j2cache</groupId><artifactId>j2cache-spring-boot2-starter</artifactId><version>2.8.0-release</version></dependency><dependency><groupId>net.oschina.j2cache</groupId><artifactId>j2cache-core</artifactId><version>2.8.0-release</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId></exclusion><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId></exclusion></exclusions></dependency></dependencies></project>
第二步:创建application.yml
server:port: 9000
# redis 通用配置, 不同的环境,需要配置不同的链接信息,
# 只需要将这段信息复制到具体环境的配置文件中进行修改即可
# 如:复制到pd-auth-server-dev.yml中将数据库名和ip改掉
pinda:redis:ip: 127.0.0.1port: 6379password:database: 0spring:cache:type: GENERICredis:host: ${pinda.redis.ip}password: ${pinda.redis.password}port: ${pinda.redis.port}database: ${pinda.redis.database}j2cache:# config-location: /j2cache.propertiesopen-spring-cache: truecache-clean-mode: passiveallow-null-values: trueredis-client: lettuce #指定redis客户端使用lettuce,也可以使用Jedisl2-cache-open: true #开启二级缓存broadcast: net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy# broadcast: jgroupsL1: #指定一级缓存提供者为caffeineprovider_class: caffeine L2: #指定二级缓存提供者为redisprovider_class: net.oschina.j2cache.cache.support.redis.SpringRedisProviderconfig_section: lettucesync_ttl_to_redis: truedefault_cache_null_object: falseserialization: fst
caffeine:properties: /caffeine.properties # 这个配置文件需要放在项目中
lettuce:mode: singlenamespace:storage: genericchannel: j2cachescheme: redishosts: ${pinda.redis.ip}:${pinda.redis.port}password: ${pinda.redis.password}database: ${pinda.redis.database}sentinelMasterId:maxTotal: 100maxIdle: 10minIdle: 10timeout: 10000
第三步:创建/resources/caffeine.properties文件
#########################################
# Caffeine configuration
# [name] = size, xxxx[s|m|h|d]
#########################################
default=2000, 2h
rx=50, 2h
第四步:创建MyController
package com.evan.controller;import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.CacheObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.List;@RestController
@RequestMapping("/cache")
public class MyController {private String key = "myKey";private String region="rx";@Autowiredprivate CacheChannel cacheChannel;@GetMapping("/getInfos")public List<String> getInfos(){CacheObject cacheObject = cacheChannel.get(region, key);if(cacheObject.getValue() == null){//缓存中没有找到,查询数据库获得List<String> data = new ArrayList<String>();data.add("info1");data.add("info2");//放入缓存cacheChannel.set(region,key,data);return data;}return (List<String>) cacheObject.getValue();}//清理指定缓存@GetMapping("/evict")public String evict(){cacheChannel.evict(region,key);return "evict success";}//检测存在那级缓存@GetMapping("/check")public String check(){int check = cacheChannel.check(region, key);return "level:" + check;}//检测缓存数据是否存在@GetMapping("/exists")public String exists(){boolean exists = cacheChannel.exists(region, key);return "exists:" + exists;}//清理指定区域的缓存@GetMapping("/clear")public String clear(){cacheChannel.clear(region);return "clear success";}
}
第五步:创建启动类
@SpringBootApplication
public class J2CacheApp {public static void main(String[] args) {SpringApplication.run(J2CacheApp.class,args);}
}
注意:由于我们当前第二级缓存使用的是redis,所以需要启动redis服务才能正常运行入门案例。
启动项目,访问地址:http://localhost:9000/cache/getInfos
重启项目,由于j2cache的一级缓存(caffeine)是进程级缓存,重启后一级缓存消失。但是二级缓存(redis)的数据还存在,再次访问上面地址,通过debug断点调试可以看到程序从redis中获取了缓存数据。
目录结构
【SpringBoot】:j2cache入门案例相关推荐
- Springboot入门案例教程
从springboot的入门案例中,我们可以体会到springboot的便捷之处,使用Spring Initializer创建一个项目,然后写一个controller层就可以运行起来,我们啥也没配 ...
- j2cache入门使用
j2cache入门使用 1 .j2cache介绍 j2cache是OSChina目前正在使用的两级缓存框架. j2cache的两级缓存结构: L1: 进程内缓存 caffeine/ehcache L2 ...
- SpringBoot整合SpringSecurity [超详细] (一)入门案例
文章目录 前言 1. 主流安全框架有哪些 2. Spring Security为什么越来越火 3. 为什么现在越来越多的人选择Spring Security 一.Spring Security 简介 ...
- SpringBoot的Web开发入门案例1
SpringBoot的Web开发入门案例1-登录和页面数据遍历读取 新建maven项目:logintest pom.xml文件: <project xmlns="http://mave ...
- SpringBoot的Web开发入门案例2—国际化
SpringBoot的Web开发入门案例2-国际化 改造logintest项目:SpringBoot的Web开发入门案例1 地址:https://blog.csdn.net/BLU_111/artic ...
- SpringBoot的Web开发入门案例9—数据访问
SpringBoot的Web开发入门案例9-数据访问 创建一个springboot项目(打包方式为jar包): 勾选Spring Web选项,勾选JDBC API和MySQL Driver pom文件 ...
- SpringBoot的Web开发入门案例7—WebMvcConfigurer配置类
SpringBoot的Web开发入门案例7-WebMvcConfigurer配置类 WebMvcConfigurer接口的几个常用方法: addViewControllers:配置请求路径和页面的映射 ...
- SpringBoot的Web开发入门案例3—异常处理
SpringBoot的Web开发入门案例3-异常处理 SpringBoot 默认404界面(由org.springframework.boot.autoconfigure.web.ErrorMvcAu ...
- SpringBoot的Web开发入门案例6—替换默认容器Tomcat
SpringBoot的Web开发入门案例6-替换默认容器Tomcat为Jetty Spring Boot默认是使用Tomcat作为内嵌的Servlet容器的,如需修改为Jetty,只要修改pom文件即 ...
最新文章
- java中字节输入流和输出流的简单使用例子
- 码云创建maven工程
- 强烈推荐Spring Web Flow权威指南
- for update引发的血案
- java中多态的例子_java中的多态案例
- 三元运算符运算(Day02)
- 展望未来计算机可以分为哪几种类型,大学计算机基础-计算机概述.ppt
- 女人不需要哲学,因为哲学不能给她们带来面包
- UI_DEV_Environment 之 StoryBook
- 电机功率与转矩/扭矩的关系
- java 开发ocx控件_Java调用ocx控件以及dll
- 126邮箱手机登录服务器密码怎么办,魅族手机登录网易126邮箱提示账号密码或协议设置不正确解决办法...
- 程序员为什么单身?细数程序员六宗罪
- MVC项目使用easyui的filebox控件上传文件
- BZOJ 2002 HNOI2010 弹飞绵羊 分块
- Android Studio 截屏
- 应用程序无法开机自启动
- 腾讯位置服务定位组件实现周边公用厕所远近排序分布图
- mac系统免费软件 GoodNotes 5.8.6 中文版 (最好的手写笔记应用)
- 想炒期货是如何开户的?