当前位置: 首页 > news >正文

网站平台建设方案的难点重点修改wordpress时区

网站平台建设方案的难点重点,修改wordpress时区,学校网站制作价格,wordpress前台增加编辑本地缓存是将数据存储在应用程序所在的本地内存中的缓存方式。既然#xff0c;已经有了 Redis 可以实现分布式缓存了#xff0c;为什么还需要本地缓存呢#xff1f;接下来#xff0c;我们一起来看。 为什么需要本地缓存#xff1f; 尽管已经有 Redis 缓存了#xff0c;但…本地缓存是将数据存储在应用程序所在的本地内存中的缓存方式。既然已经有了 Redis 可以实现分布式缓存了为什么还需要本地缓存呢接下来我们一起来看。 为什么需要本地缓存 尽管已经有 Redis 缓存了但本地缓存也是非常有必要的因为它有以下优点 速度优势本地缓存直接利用本地内存访问速度非常快能够显著降低数据访问延迟。 减少网络开销使用本地缓存可以减少与远程缓存如 Redis之间的数据交互从而降低网络 I/O 开销。 降低服务器压力本地缓存能够分担服务器的数据访问压力提高系统的整体稳定性。 因此在生产环境中我们通常使用本地缓存Redis 缓存一起组合成多级缓存来共同保证程序的运行效率。 多级缓存 多级缓存是一种缓存架构策略它使用多个层次的缓存来存储数据以提高数据访问速度和系统性能最简单的多级缓存就是由本地缓存 Redis 分布式缓存组成的如图所示 多级缓存在获取时的实现代码如下 public Object getFromCache(String key) { // 先从本地缓存中查找 Cache.ValueWrapper localCacheValue cacheManager.getCache(“localCache”).get(key); if (localCacheValue! null) { return localCacheValue.get(); } // 如果本地缓存未命中从 Redis 中查找 Object redisValue redisTemplate.opsForValue().get(key); if (redisValue! null) { // 将 Redis 中的数据放入本地缓存 cacheManager.getCache(“localCache”).put(key, redisValue); return redisValue; } return null; } 本地缓存的实现 本地缓存常见的方式实现有以下几种 Ehcache Caffeine Guava Cache 它们的基本使用如下。 1.Ehcache 1.1 添加依赖 在 pom.xml 文件中添加 Ehcache 依赖 org.springframework.boot spring-boot-starter-cache org.ehcache ehcache 1.2 配置 Ehcache 在 src/main/resources 目录下创建 ehcache.xml 文件 1.3 启用缓存 在 Spring Boot 应用的主类或配置类上添加 EnableCaching 注解 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; SpringBootApplication EnableCaching public class CacheApplication { public static void main(String[] args) { SpringApplication.run(CacheApplication.class, args); } } 1.4 使用缓存 创建一个服务类并使用 Cacheable 注解 import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; Service public class MyService { Cacheable(value myCache, key #id) public String getData(String id) {// 模拟耗时操作try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return Data for id; }} 2.Caffeine 2.1 添加依赖 在 pom.xml 文件中添加 Caffeine 依赖 org.springframework.boot spring-boot-starter-cache com.github.ben-manes.caffeine caffeine 2.2 启用缓存 在 Spring Boot 应用的主类或配置类上添加 EnableCaching 注解 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; SpringBootApplication EnableCaching public class CacheApplication { public static void main(String[] args) { SpringApplication.run(CacheApplication.class, args); } } 2.3 配置 Caffeine 缓存 创建一个配置类来配置 Caffeine 缓存 import com.github.benmanes.caffeine.cache.Caffeine; import org.springframework.cache.CacheManager; import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration public class CacheConfig { Bean public CacheManager cacheManager() {CaffeineCacheManager cacheManager new CaffeineCacheManager(myCache);cacheManager.setCaffeine(Caffeine.newBuilder().maximumSize(1000).expireAfterWrite(120, TimeUnit.SECONDS));return cacheManager; }} 2.4 使用缓存 创建一个服务类并使用 Cacheable 注解 import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; Service public class MyService { Cacheable(value myCache, key #id) public String getData(String id) {// 模拟耗时操作try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return Data for id; }} 3.Guava Cache 3.1 添加依赖 在 pom.xml 文件中添加 Guava 依赖 org.springframework.boot spring-boot-starter-cache com.google.guava guava 3.2 启用缓存 在 Spring Boot 应用的主类或配置类上添加 EnableCaching 注解 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; SpringBootApplication EnableCaching public class CacheApplication { public static void main(String[] args) { SpringApplication.run(CacheApplication.class, args); } } 3.3 配置 Guava 缓存 创建一个配置类来配置 Guava 缓存 import com.google.common.cache.CacheBuilder; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.cache.concurrent.ConcurrentMapCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; Configuration public class CacheConfig { Bean public CacheManager cacheManager() {ConcurrentMapCacheManager cacheManager new ConcurrentMapCacheManager() {Overrideprotected Cache createConcurrentMapCache(String name) {return new ConcurrentMapCache(name,CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(120, TimeUnit.SECONDS).build().asMap(), false);}};return cacheManager; }} 3.4 使用缓存 创建一个服务类并使用 Cacheable 注解 import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; Service public class MyService { Cacheable(value myCache, key #id) public String getData(String id) {// 模拟耗时操作try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return Data for id; }} 知识扩展Cacheable、CachePut、CacheEvict 在 Spring 框架中Cacheable、CachePut 和 CacheEvict 是用于缓存管理的注解它们的含义如下 Cacheable用于声明一个方法的返回值是可以被缓存的。当方法被调用时Spring Cache 会先检查缓存中是否存在相应的数据。如果存在则直接返回缓存中的数据避免重复执行方法如果不存在则执行方法并将返回值存入缓存中。它的使用示例如下 Cacheable(value “users”, key “#id”) public User getUserById(String id) { // 模拟从数据库中获取用户信息 System.out.println(Fetching user from database: id); return new User(id, User Name id); } CachePut用于更新缓存中的数据。与 Cacheable 不同CachePut 注解的方法总是会执行并将返回值更新到缓存中。无论缓存中是否存在相应的数据该方法都会执行并将新的数据存入缓存中如果缓存中已存在数据则覆盖它。它的使用示例如下 CachePut(value “users”, key “#user.id”) public User updateUser(User user) { // 模拟更新数据库中的用户信息 System.out.println(Updating user in database: user.getId()); // 假设更新成功 return user; } CacheEvict用于删除缓存中的数据。当方法被调用时指定的缓存项将被删除。这可以用于清除旧数据或使缓存项失效。它的使用示例如下 CacheEvict(value “users”, key “#id”) public void deleteUser(String id) { // 模拟从数据库中删除用户信息 System.out.println(Deleting user from database: id); } // 清除整个缓存而不仅仅是特定的条目 CacheEvict(value “users”, allEntries true) public void clearAllUsersCache() { System.out.println(“Clearing all users cache”); } 小结 生产环境通常会使用本地缓存 Redis 缓存一起实现多级缓存以提升程序的运行效率而本地缓存的常见实现有 Ehcache、Caffeine、Guava Cache 等。然而凡事有利就有弊那么多级缓存最大的问题就是数据一致性问题对于多级缓存的数据一致性问题要如何保证呢
http://www.hkea.cn/news/14527880/

相关文章:

  • 网站不备案做电影网站界面设计优秀的网站有哪些
  • 手机网站开发方式中小型网站建设咨询
  • 虚拟主机怎么搭建网站wordpress 全站404
  • 太原网站制作定制开发扬州市住房建设局网站
  • 跨境电商建站好的品牌设计网站有哪些
  • 海北营销网站建设公司怎么做像天猫类似的网站
  • 网站大部分都是jsp做的咸阳 网站建设
  • 网站规划和建设方案申请个人企业邮箱
  • 做电气设计有哪些好的网站织梦网站怎么加入引导页
  • 网站建设结构方案中英文外贸网站源码
  • 广州网站程序开发希爱力
  • 计算机网站建设体会巴中市建设局网站
  • 西安监控系统网站开发谷歌搜索引擎免费入口镜像
  • 外贸联系网站精品课程网站开发关键技术
  • 建设部网站监理工程师报名wordpress欢迎主题
  • 建设银行北京分行招聘网站网站建设佰首选金手指五
  • 专门做超市dm网站鲜花网站怎么做
  • 盐城整站优化邢台网约车
  • 无锡网站建设培训学校wordpress搭建微信小程序
  • 个人网站首页公司网站建设费用
  • 成都网络推广网站营销型网站与展示型网站
  • 招聘网站怎么做seo摄影网站规划设计书
  • 烟台网站搭建wordpress自建表格
  • 建大网站首页商派商城网站建设
  • 手机怎么上wap网站ui设计通常是指
  • 做网站 钱重庆网站建设的好处
  • 电商网站开发流程文档配色设计网站推荐
  • 广西响应式网站哪家好龙城建设网站公司
  • 专业 网站设计公司价格南宁建站模板厂家
  • 手机网站页面模板wordpress域名设置方法