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

重庆哪家网站网站优化企业排名

重庆哪家网站,网站优化企业排名,房地产招新人的坑,做仪表行业推广有哪些网站在 Spring 框架中#xff0c;可通过多种方式配置缓存具体行为#xff0c;常见配置方法如下。 1. 缓存管理器#xff08;CacheManager#xff09;配置 基于内存的缓存管理器配置#xff08;以SimpleCacheManager为例#xff09; SimpleCacheManager 是 Spring 提供的简单…在 Spring 框架中可通过多种方式配置缓存具体行为常见配置方法如下。 1. 缓存管理器CacheManager配置 基于内存的缓存管理器配置以SimpleCacheManager为例 SimpleCacheManager 是 Spring 提供的简单缓存管理器用于管理内存缓存。适用于开发和测试阶段或数据量小、缓存一致性要求不高的场景。 首先需要在Spring配置文件如applicationContext.xml或者通过Java配置类使用Configuration注解来配置SimpleCacheManager。 下面代码定义了一个CacheManager类型的bean。通过SimpleCacheManager创建了一个缓存管理器并设置了两个基于ConcurrentMapCache的缓存名称分别为userCache和productCache。这些缓存名称可以在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;ConfigurationEnableCachingpublic 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-redisSpring Boot 项目。再用 Java 配置类配置JedisConnectionFactory和RedisCacheManager。 下面配置首先创建了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;ConfigurationEnableCachingpublic class RedisCacheConfig {Beanpublic RedisConnectionFactory redisConnectionFactory() {JedisConnectionFactory jedisConnectionFactory new JedisConnectionFactory();// 可以配置Redis服务器的主机名、端口等信息jedisConnectionFactory.setHostName(localhost);jedisConnectionFactory.setPort(6379);return jedisConnectionFactory;}Beanpublic RedisTemplateString, Object redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplateString, 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属性来指定缓存键。可以使用SpELSpring Expression Language表达式来动态生成缓存键。 在查询用户信息的方法中以用户id和lastName为缓存键。代码中key属性表达式#user.id - #user.lastName将用户对象的id和lastName拼接成字符串作缓存键。如此即便有多个用户对象只要id和lastName组合不同就会存于不同缓存位置。 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;ConfigurationEnableCachingpublic 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.w-s-a.com/news/749938/

相关文章:

  • 网站建设建设公司哪家好seo网站优化推广
  • 网站服务器组建网站案例上海
  • 盘锦949公社最新招聘优化大师免费版
  • 国外有哪些网站是做弱电的中国国家培训网正规吗
  • 30分钟网站建设教程视频全屋整装120平米的多少钱
  • 生成链接的网站aso优化平台
  • 策划网站建设方案电商扶贫网站建设
  • 网站策划建设方法企业网站建设问题研究
  • 昆明专业网站建设的公司帮别人制作wordpress赚钱吗
  • 高校校园网站建设天水市建设局网站公告
  • 北京网站建设需要花多少钱企业建设网站的目的是
  • 网站模板 免费百度seo优化招聘
  • 过年做那些网站能致富怎样免费建立自己网站
  • 网站去哪里备案长沙网络推广
  • 企业网站规划书vue适合什么样的网站开发
  • 个人网站备案名字网站设计的提案
  • 网站自己做还是找人做常州钟楼区邹区建设局网站
  • 网站文件上传wordpress修改asp做微网站
  • 妇女之家网站建设方案英语不行如何编程做网站
  • 深圳企业网站建设推广服务网站托管一年多少钱
  • wordpress 百度地图api网络seo天津
  • 网站营销咨询顾问餐饮加盟网站建设方案
  • 网站后台管理系统的重要技术指标wordpress下单邮件通知的实现
  • 通化县住房和城乡建设局网站定制网站收费
  • 湖北做网站教程哪家好成都网站建设询q479185700上快
  • 网站的seo方案鹰潭做网站的公司
  • 高级室内设计网站太原网站设计费用
  • 智信建设职业培训学校网站深圳做网站建设开发
  • 宣城市住房和城乡建设局网站网站界面设计专利
  • 免费个人网站建站申请如何做内网网站