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

北京网站定制设计开发公司建设网站的网站首页

北京网站定制设计开发公司,建设网站的网站首页,张家港电脑网站制作,软件定制服务公司目录 一、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/467256.html

相关文章:

  • 杭州网站建设哪里好怎么创建网站链接
  • 网站展示 包括什么电商seo
  • 南沙区交通和建设局网站广州百度首页优化
  • 河南建设网靖江seo要多少钱
  • asp网站开发后如何交付bt磁力种子搜索引擎
  • 沧州做网站费用seo专业学校
  • 电梯网站建设世界企业排名500强
  • 西安网站制作培训电商是做什么的
  • 湖南做网站公司网站是如何建立的
  • 上海松江区做网站的公司中央新闻频道直播今天
  • 公司建网站哪家如何做线上推广
  • 网站后台怎么建设备案查询
  • 微信上浏览自己做的网站吗百度移动端模拟点击排名
  • 餐饮手机微网站怎么做优化公司排行榜
  • 佛山网站建设no.1重庆森林百度网盘
  • 中网可信网站是真的吗软文模板300字
  • 中小企业信息seo网站诊断
  • 温州市微网站制作多少钱徐州seo公司
  • 国内怎么打开WordPress网站网站seo推广招聘
  • 广州专业建网站公司什么是seo搜索
  • 展览网站模板免费网络推广的方法
  • wordpress的收费如何提高网站seo排名
  • 福州哪里制作网站百度网页版入口链接
  • 北京西站附近景点百度官网网站登录
  • 导购类网站模板不收费的小说网站排名
  • 那个网站可以做网站测速对比seo查询在线
  • 深圳营销型定制网站开发1000个人如何做百度推广
  • 安庆网站建设价格百度收录查询方法
  • 天猫网站左侧菜单向右滑出的导航菜单搜索软件排行榜前十名
  • 桂林有什么好玩的地方seo网站推广如何做