欢迎光临
我们一直在努力

springboot使用h2数据库,springboot使用log4j2

springboot使用j2cache缓存

1pom文件引入

<?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”> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.7.RELEASE</version> <relativePath/> <!– lookup parent from repository –> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>test_j2cache</artifactId> <dependencies> <dependency> <groupId>net.oschina.j2cache</groupId> <artifactId>j2cache-spring-boot2-starter</artifactId> <version>2.7.0-release</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies></project>

2:配置文件

2.1application.properties配置文件配置

server.port=8080#cache#j2cache配置文件路径j2cache.config-location=classpath:j2cache.properties#开启spring cache支持j2cache.open-spring-cache=true

?2.2???j2cache.properties配置文件

j2cache.broadcast = lettuce# jgroups propertiesjgroups.channel.name = j2cachej2cache.L1.provider_class = caffeinej2cache.L2.provider_class = lettuce# 缓存空对象 (default false)#j2cache.default_cache_null_object = false#redis缓存序列化方式,fst、kyro、json、fastjson、javaj2cache.serialization = fastjsoncaffeine.properties = /caffeine.propertieslettuce.namespace =lettuce.mode = singlelettuce.cluster_name = j2cachelettuce.storage = genericlettuce.channel = j2cachelettuce.channel.host =lettuce.scheme = redislettuce.hosts = 172.23.0.182:6379lettuce.password =lettuce.database = 3lettuce.sentinelMasterId =lettuce.maxTotal = 100lettuce.maxIdle = 10lettuce.numTestsPerEvictionRun = 10lettuce.softMinEvictableIdleTimeMillis = 10lettuce.minIdle = 10lettuce.maxWaitMillis = 5000lettuce.lifo = falselettuce.minEvictableIdleTimeMillis = 60000lettuce.timeout = 10000lettuce.testOnBorrow = truelettuce.testOnReturn = falselettuce.testWhileIdle = truelettuce.timeBetweenEvictionRunsMillis = 300000lettuce.blockWhenExhausted = false

本地缓存Caffeine配置?caffeine.properties

########################################## Caffeine configuration# [name] = size, xxxx[s|m|h|d]#########################################default=1000, 30mtestCache=10000, 6s

测试代码

package com.cache.redis;import com.cache.redis.model.Person;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.ApplicationContext;import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication@EnableCachingpublic class DemoApplication {public static void main(String[] args) {ApplicationContext app = SpringApplication.run(DemoApplication.class, args);UserService userService = app.getBean(UserService.class);Person person = new Person(1L,”自觉的板栗”,”18″,”男”);System.out.println(“插入用户,新增缓存”);userService.savePerson(person);System.out.println(“第一次获取用户,存在缓存就从缓存返回,不存在就从数据库取”);Person dbPerson = userService.getUserById(1L);System.out.println(dbPerson);System.out.println(“第二次获取用户”);dbPerson = userService.getUserById(1L);System.out.println(dbPerson);System.out.println(“更新用户,更新缓存”);person.setAge(“88”);userService.savePerson(person);System.out.println(“第二次获取用户,看缓存是否有变化”);dbPerson = userService.getUserById(1L);System.out.println(dbPerson);System.out.println(“删除用户,删除缓存”);userService.delPerson(1L);System.out.println(“第三次再次获取用户,看缓存是否有变化”);dbPerson = userService.getUserById(1L);System.out.println(dbPerson);}} package com.cache.redis;import com.cache.redis.model.Person;import org.springframework.cache.annotation.CacheConfig;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.CachePut;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;/** * @author: tsddx chen * @description: * @date: 2019/2/14 */@Service@CacheConfig(cacheNames = “person”)public class UserService { private Person person = null; @Cacheable(key = “#id”) public Person getUserById(Long id){ //如果没走缓存,会打印下面这句话 System.out.println(“=>操作数据库,根据id获取用户信息”); return this.person; } @CachePut(key = “#person.id”) public Person savePerson(Person person){ System.out.println(“=>操作数据库保存用户数据”); this.person = person; return this.person; } @CacheEvict(key = “#id”) public void delPerson(Long id){ System.out.println(“=>操作数据库删除用户数据”); person = null; }}

?

?

?

package com.cache.redis.model;import java.io.Serializable;/** * @author: tsddx chen * @description: * @date: 2019/2/14 */public class Person implements Serializable{ private Long id; private String name; private String age; private String sex; public Person() { } public Person(Long id, String name, String age, String sex) { this.id = id; this.name = name; this.age = age; this.sex = sex; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return “Person{” + “id=” + id + “, name='” + name + ‘\” + “, age='” + age + ‘\” + “, sex='” + sex + ‘\” + ‘}’; }}

?

?

47714745

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。