当前位置: 首页> 科技> 互联网 > 企业网站备案需要什么资料_网络营销推广的特点有_今日头条淄博新闻_百度品牌广告多少钱一个月

企业网站备案需要什么资料_网络营销推广的特点有_今日头条淄博新闻_百度品牌广告多少钱一个月

时间:2025/7/27 8:38:08来源:https://blog.csdn.net/C_V_Better/article/details/147139284 浏览次数:0次
企业网站备案需要什么资料_网络营销推广的特点有_今日头条淄博新闻_百度品牌广告多少钱一个月

目录

    • 一、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 作为二级缓存,提供数据的持久化和分布式共享。这种架构在提高数据访问速度、减少数据库压力方面具有显著优势。希望本文的示例和讲解对您有所帮助,如果您在实现二级缓存时有任何疑问,欢迎随时交流探讨!

关键字:企业网站备案需要什么资料_网络营销推广的特点有_今日头条淄博新闻_百度品牌广告多少钱一个月

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: