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

做网站的前景如何韶关seo

做网站的前景如何,韶关seo,域名注册了如何做网站,营销网店推广的软文目录 一、Redis与Caffeine的结合使用(一)引入依赖(二)配置缓存(三)配置 Caffeine 缓存(四)配置 Redis 缓存(五)使用缓存 二、总结 一、Redis与Caffeine的结合…

目录

    • 一、Redis与Caffeine的结合使用
      • (一)引入依赖
      • (二)配置缓存
      • (三)配置 Caffeine 缓存
      • (四)配置 Redis 缓存
      • (五)使用缓存
    • 二、总结

一、Redis与Caffeine的结合使用

Redis 和 Caffeine 是两种广受欢迎的缓存技术,Redis 是一个高性能的键值数据库,提供了数据持久化和分布式缓存功能;Caffeine 是一个高性能的缓存库,访问速度快,能够提供接近内存的访问效率。将 Redis 和 Caffeine 结合起来,可以构建一个高效的二级缓存解决方案。Caffeine 作为一级缓存,提供快速访问,而 Redis 作为二级缓存,解决数据共享和持久化问题。

(一)引入依赖

pom.xml 文件中添加以下依赖:

<dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId><version>2.9.2</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-cache</artifactId>
</dependency>
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.8.1</version>
</dependency>

(二)配置缓存

application.yml 中配置 Redis 的连接信息:

spring:redis:host: 127.0.0.1port: 6379database: 0timeout: 10000mslettuce:pool:max-active: 8max-wait: -1msmax-idle: 8min-idle: 0

(三)配置 Caffeine 缓存

import com.github.ben.manes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@EnableCaching
public class CaffeineConfig {@Beanpublic CacheManager caffeineCacheManager() {return new CaffeineCacheManager(Caffeine.newBuilder().initialCapacity(128).maximumSize(1024).expireAfterWrite(60, TimeUnit.SECONDS).build());}
}

(四)配置 Redis 缓存

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
@EnableCaching
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());return template;}
}

(五)使用缓存

在业务代码中,可以使用 @Cacheable@CachePut@CacheEvict 注解来简化缓存操作。

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class OrderService {@Cacheable(cacheNames = "order", key = "#id")public Order getOrderById(Long id) {// 查询数据库return orderMapper.selectById(id);}@CachePut(cacheNames = "order", key = "#order.id")public Order updateOrder(Order order) {// 更新数据库orderMapper.updateById(order);// 更新 RedisredisTemplate.opsForValue().set("order:" + order.getId(), order, 120, TimeUnit.SECONDS);return order;}@CacheEvict(cacheNames = "order", key = "#id")public void deleteOrder(Long id) {// 删除数据库记录orderMapper.deleteById(id);// 删除 Redis 缓存redisTemplate.delete("order:" + id);}
}

二、总结

通过结合 Redis 和 Caffeine,可以构建一个高效的二级缓存解决方案。Caffeine 作为一级缓存,提供快速的本地访问;Redis 作为二级缓存,提供数据的持久化和分布式共享。这种架构在提高数据访问速度、减少数据库压力方面具有显著优势。希望本文的示例和讲解对您有所帮助,如果您在实现二级缓存时有任何疑问,欢迎随时交流探讨!

http://www.dtcms.com/wzjs/174249.html

相关文章:

  • 吴忠住房和城乡建设局网站海外市场推广策略
  • 网站删除留言板功能删除地推拉新app推广接单平台免费
  • 城市建设招标网站佛山网站建设
  • wordpress微信公众号关注登陆sem和seo是什么
  • 重庆做网站建设找谁百度关键词排名十大排名
  • 怎么做网站的动效推广赚佣金
  • 政府统一建设网站的提议关键词查询网
  • 免费做网站空间什么软件可以推广
  • 电子商务网站开发开发背景高级seo是什么职位
  • 个人网站 icp微信代运营
  • 做推广的网站吗成都网站建设制作公司
  • 沧州做企业网站合肥瑶海区房价
  • 简书网站开发重庆百度推广优化
  • 武汉网站建设案例教程最新域名查询
  • wordpress的中文插件百度网站优化
  • 58网站为啥做不好潍坊百度快速排名优化
  • 企业网站建设服务公司武汉网络关键词排名
  • 长春网站如何制作中国营销网官网
  • 任丘网站建设价格刷网站seo排名软件
  • 免费word在线编辑优化网站seo
  • 南京建站公司模板免费网站推广方式
  • 网站备案大概需要多久seo优化一般优化哪些方面
  • 有哪些做的很漂亮的网站百度一下你就知道
  • 电子商务网站建设讯息免费网站建设seo
  • 手机营销网站模板百度首页关键词优化
  • wordpress筛选分类日照seo优化
  • 怎么免费的安装wordpress主题杭州专业seo公司
  • php 用什么做网站服务器吗企业关键词优化推荐
  • 网站设计搜索栏怎么做seo推广怎么入门
  • 做网站石材推销seo关键词排名实用软件