基于ehcache+redis实现分布式缓存架构(上篇+下篇) - Go语言中文社区

基于ehcache+redis实现分布式缓存架构(上篇+下篇)


一、使用缓存的目的和好处

使用缓存可以减轻数据库的访问压力,提高数据的响应速度。

二、ehcache与springBoot2.0整合,并使用ehcache实现缓存。

1.在pom.xml中添加ehcache的依赖。

<!--开启 cache 缓存 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>
		<!-- ehcache缓存 -->
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache</artifactId>
			<version>2.9.1</version><!--$NO-MVN-MAN-VER$ -->
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>

2.在springBoot的启动文件中开启缓存

@MapperScan(basePackages = { "com.itmayiedu.mapper" })
@EnableCaching  //开启缓存
@SpringBootApplication
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}

}

3.在dao层添加两个注解

//配置缓存基本信息
@CacheConfig(cacheNames = "userCache")
public interface UserMapper {
	@Select("SELECT ID ,NAME,AGE FROM student where id=#{id}")
	//将查询的信息放入缓存中
	@Cacheable
	List<Users> getUser(@Param("id") Long id);
}

4.最后一步:在添加application.yml和app1_ehcache.xml添加如下配置:

###端口号配置
server:
  port: 8081
###数据库配置  
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password:
    driver-class-name: com.mysql.jdbc.Driver
    test-while-idle: true
    test-on-borrow: true
    validation-query: SELECT 1 FROM DUAL
    time-between-eviction-runs-millis: 300000
    min-evictable-idle-time-millis: 1800000
  # 缓存配置读取
  cache:
    type: ehcache
    ehcache:
      config: classpath:app1_ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
	<diskStore path="java.io.tmpdir/ehcache-rmi-4000" />
	<!-- 默认缓存 -->
	<defaultCache maxElementsInMemory="1000" eternal="true"
		timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
		diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
		diskPersistent="true" diskExpiryThreadIntervalSeconds="120"
		memoryStoreEvictionPolicy="LRU">
	</defaultCache>
	<!-- userCache缓存 -->
	<cache name="userCache" maxElementsInMemory="1000" eternal="false"
		timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
		diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
		diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
		memoryStoreEvictionPolicy="LRU">
		<cacheEventListenerFactory
			class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
		<!-- 用于在初始化缓存,以及自动设置 -->
		<bootstrapCacheLoaderFactory
			class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />
	</cache>
</ehcache>

 

 

 

5 配置完成,在浏览器中验证,第一次访问时的数据,然后你去修改数据库,你会发现数据依然不变,说明查询的结果已经放入缓存中了。

总结:上面你应该会发现存在一个问题,如何解决缓存和数据库中的数据不同步的问题?其实我们可以想象为什么数据会不同步,是因为我们做了修改或者更新导致的。那么我们在做数据更新或者修改时,需要把原来的缓存清除。或者写个定时任务去检查缓存中的数据与数据库中的数据是否同步,假如不同步我们需要清理缓存,术语叫做健康检查。

补充一句:ehcache属于jvm内置缓存,适用于单个应用,但是也支持分布式应用(不推荐)。不早了,大家晚安。明天接着写下篇!

基于ehcache+redis实现分布式缓存架构(下篇)

话不多说我们直接给出框架图

over!

 

 

 

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_29252775/article/details/99687983
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 22:49:03
  • 阅读 ( 1100 )
  • 分类:架构

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢