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

电子商务企业网站的推广方式个人做网站的注意事项

电子商务企业网站的推广方式,个人做网站的注意事项,做电子商城网站的,网站建设优化公司在 Spring 框架中,可通过多种方式配置缓存具体行为,常见配置方法如下。 1. 缓存管理器(CacheManager)配置 基于内存的缓存管理器配置(以SimpleCacheManager为例) SimpleCacheManager 是 Spring 提供的简单…

在 Spring 框架中,可通过多种方式配置缓存具体行为,常见配置方法如下。

在这里插入图片描述

1. 缓存管理器(CacheManager)配置

基于内存的缓存管理器配置(以SimpleCacheManager为例)

SimpleCacheManager 是 Spring 提供的简单缓存管理器,用于管理内存缓存。适用于开发和测试阶段,或数据量小、缓存一致性要求不高的场景。

首先需要在Spring配置文件(如applicationContext.xml)或者通过Java配置类(使用@Configuration注解)来配置SimpleCacheManager

下面代码定义了一个CacheManager类型的bean。通过SimpleCacheManager创建了一个缓存管理器,并设置了两个基于ConcurrentMapCache的缓存,名称分别为userCacheproductCache。这些缓存名称可以在@Cacheable@CachePut@CacheEvict等注解的cacheNames属性中使用。

  import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.concurrent.ConcurrentMapCache;import org.springframework.cache.support.SimpleCacheManager;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.Arrays;@Configuration@EnableCachingpublic class CacheConfig {@Beanpublic CacheManager cacheManager() {SimpleCacheManager cacheManager = new SimpleCacheManager();cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("userCache"),new ConcurrentMapCache("productCache")));return cacheManager;}}

基于Redis的缓存管理器配置(以JedisConnectionFactory和RedisCacheManager为例)

Redis是一个高性能的分布式缓存数据库,在生产环境中被广泛使用。通过配置Redis缓存管理器,可以将Spring应用的缓存数据存储到Redis中,实现数据的共享和高效访问。

添加 Redis 依赖如spring-boot-starter-data-redis(Spring Boot 项目)。再用 Java 配置类配置JedisConnectionFactoryRedisCacheManager

下面配置首先创建了JedisConnectionFactory,用于建立与Redis服务器的连接。可以在其中设置Redis服务器的主机名、端口等信息。然后创建了RedisTemplate,用于在Redis中进行数据的读写操作,同时设置了键和值的序列化方式。最后通过RedisCacheManager创建了缓存管理器,它将使用之前配置的RedisConnectionFactory来管理缓存数据与Redis的交互。

  import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration@EnableCachingpublic class RedisCacheConfig {@Beanpublic RedisConnectionFactory redisConnectionFactory() {JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();// 可以配置Redis服务器的主机名、端口等信息jedisConnectionFactory.setHostName("localhost");jedisConnectionFactory.setPort(6379);return jedisConnectionFactory;}@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(redisConnectionFactory);redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());return redisTemplate;}@Beanpublic CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {RedisCacheManager cacheManager = RedisCacheManager.create(redisConnectionFactory);return cacheManager;}}

2. 缓存注解参数配置

自定义缓存键(Key)

通过自定义缓存键,可以更精确地控制缓存数据的存储和检索。合理的缓存键设计可以避免缓存数据的冲突,提高缓存的命中率。

@Cacheable@CachePut@CacheEvict等注解中使用key属性来指定缓存键。可以使用SpEL(Spring Expression Language)表达式来动态生成缓存键。

在查询用户信息的方法中,以用户idlastName为缓存键。代码中,key属性表达式#user.id + '-' + #user.lastName将用户对象的idlastName拼接成字符串作缓存键。如此,即便有多个用户对象,只要idlastName组合不同,就会存于不同缓存位置。

  @Cacheable(cacheNames = "userCache", key = "#user.id + '-' + #user.lastName")public User getUser(User user) {// 从数据库查询用户信息的逻辑return userRepository.findByUser(user);}

缓存条件(Condition)配置

缓存条件配置允许根据特定的条件来决定是否进行缓存操作。这在一些复杂的业务场景中非常有用,例如只缓存满足一定条件的数据,或者根据业务规则来决定是否更新或清除缓存。

使用@Cacheable@CachePut@CacheEvict注解的condition属性,通过SpEL表达式来指定条件。

例如只缓存年龄大于 18 岁的用户信息,年龄大于 18 岁时查询结果才被缓存。若用户年龄小于等于 18 岁,每次调用方法执行数据库查询,不使用缓存。

  @Cacheable(cacheNames = "userCache", condition = "#user.age > 18")public User getUser(User user) {// 从数据库查询用户信息的逻辑return userRepository.findByUser(user);}

3. 缓存过期时间配置

基于特定缓存实现的过期时间设置

不同的缓存实现技术(如Ehcache、Redis等)有自己的过期时间设置方式。对于基于内存的缓存,过期时间设置可能相对简单;而对于分布式缓存,可能需要考虑更多的因素,如数据一致性等。

在Redis中,可以通过在存储缓存数据时设置过期时间来实现。在Spring应用中,当使用RedisCacheManager时,RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(30))设置了默认的缓存过期时间为30分钟。所有存储到Redis中的缓存数据,如果没有单独设置过期时间,将在30分钟后自动过期。

  import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.EnableCaching;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheConfiguration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializationContext;import org.springframework.data.redis.serializer.StringRedisSerializer;import java.time.Duration;@Configuration@EnableCachingpublic class RedisCacheExpirationConfig {@Beanpublic CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(30)).serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));return RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(defaultCacheConfig).build();}}
http://www.yayakq.cn/news/795817/

相关文章:

  • 哪个网站做的win10系统好安徽水安建设集团网站
  • 大气集团网站网站关键字优化简介
  • 室内设计效果图素材网站淘宝网站建设策划案
  • 企业网站建设免备案wordpress表单邮件
  • 网站三要网站开发项目的心得体会
  • 丰台区网站建设常熟建设合同备案在哪个网站
  • 一分钟建设网站做导购网站如何获利
  • 邢台网站建设行情cms 网站后台
  • 广告公司网站制作稷山网站制作
  • jquery做的装修网站做百度联盟怎么才能创建多个网站
  • 奢侈品 网站建设方案广州网站建设网站优化推广
  • 陕煤化建设集团网站矿建二公司免费建立网站步骤
  • 做一个公司网站一般多少钱服饰网站 模板
  • 公司网站建设对公司的重要性c2c电子商务网站的建站目的
  • 什么是网站风格免费网站建设特色
  • 网上书店网网站建设网站排名下降了怎么办
  • 网站建设招聘系统有什么教做甜品的网站
  • 建网站合同重庆找工作最新招聘信息
  • 网站建设公司新闻一般的网络课程设计应包括课程设计和
  • 让自己的电脑做网站的服务器旅游网站建设方
  • 网站底部版权怎么做百度如何快速收录
  • 徐州网站平台安装wordpress视频教程
  • wordpress迁移站点阿里云国际站官网
  • 使用dw做门户网站南安淘宝网站建设
  • 手机互动网站建设wordpress长文档分页
  • 北京公司网站建设费用中小型网站建设公司
  • 网站过期就可以抢注如何做网站左侧导航条
  • 资源下载类网站源码局网站建设工作总结
  • 东莞市建设局质量监督网站企业做网站的公司有哪些
  • wordpress配置文件下载抖音seo是什么意思