<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>net.oschina.j2cachegroupId>
<artifactId>j2cache-coreartifactId>
<version>2.8.4-releaseversion>
dependency>
<dependency>
<groupId>net.oschina.j2cachegroupId>
<artifactId>j2cache-spring-boot2-starterartifactId>
<version>2.8.0-releaseversion>
dependency>
<dependency>
<groupId>net.sf.ehcachegroupId>
<artifactId>ehcacheartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.3version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.28version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.2.6version>
dependency>
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
username: user
password: 123456
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# j2cache相关配置,指定j2cache的
j2cache:
config-location: j2cache.properties
# j2cache.properties 的完整配置在 j2cache-core 的 jar 包中有,可参照配置
# 一级缓存
j2cache.L1.provider_class = ehcache
ehcache.configXml = ehcache.xml
# 设置是否启用二级缓存
j2cache.l2-cache-open = true
# 二级缓存
j2cache.L2.provider_class = net.oschina.j2cache.cache.support.redis.SpringRedisProvider
j2cache.L2.config_section = redis
redis.hosts = localhost:6379
redis.password = hd123
# redis缓存的key的命名空间,默认为空
redis.namespace =
# 一级缓存中的数据如何到达二级缓存
j2cache.broadcast = net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy
<ehcache updateCheck="false" dynamicConfig="false">
<diskStore path="java.io.tmpdir"/>
<cacheManagerEventListenerFactory class="" properties=""/>
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="1800"
overflowToDisk="true">
defaultCache>
<cache name="example"
maxElementsInMemory="5000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="1800"
overflowToDisk="false"
>
cache>
ehcache>
import lombok.*;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book implements Serializable {
private String id;
private String name;
private String description;
private Float price;
}
// BookService.java
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.springboot.entity.Book;
public interface BookService extends IService<Book> {
Book getCacheById(String id);
String checkCacheById(String id);
}
// BookServiceImpl.java
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.dao.BookDao;
import com.example.springboot.entity.Book;
import com.example.springboot.service.BookService;
import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.CacheObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements BookService {
@Autowired
private CacheChannel cacheChannel;
@Override
public Book getCacheById(String id) {
cacheChannel.set("j2Cache_", id, getById(id));
return null;
}
@Override
public String checkCacheById(String id) {
String aDefault = cacheChannel.get("j2Cache_", id).asString();
return aDefault;
}
}
import com.example.springboot.entity.Book;
import com.example.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("{id}")
public Book getById(@PathVariable String id){
return bookService.getCacheById(id);
}
@PostMapping
public String checkById(@RequestBody Book book){
return bookService.checkCacheById(book.getId());
}
}
往缓存中放数据:
从缓存中取数据: