成都网站怎么推广,centos7更新Wordpress,大数据营销工具,公司怎么做网页网站Spring Framework是领先的全堆栈Java/JEE应用程序框架。它提供了一个轻量级容器和一个通过使用依赖注入、AOP和可移植服务抽象实现的非侵入性编程模型。
NoSQL存储系统为传统RDBMS提供了一种横向可扩展性和速度的替代方案。就实现而言#xff0c;键值存储代表NoSQL空间中最大…Spring Framework是领先的全堆栈Java/JEE应用程序框架。它提供了一个轻量级容器和一个通过使用依赖注入、AOP和可移植服务抽象实现的非侵入性编程模型。
NoSQL存储系统为传统RDBMS提供了一种横向可扩展性和速度的替代方案。就实现而言键值存储代表NoSQL空间中最大和最古老的成员之一。
Spring Data RedisSDR框架通过Spring卓越的基础架构支持消除了与存储交互所需的冗余任务和样板代码从而使编写使用Redis键值存储的Spring应用程序变得容易。
一、Redis Cache
Spring Data Redis在org.springframework.Data.Redis.Cache包中提供了Spring Framework的Cache 抽象的实现。要使用Redis作为备份实现请将RedisCacheManager添加到配置中如下所示
Bean
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {return RedisCacheManager.create(connectionFactory);
}
可以使用RedisCacheManager配置RedisCacheManager行为。RedisCacheManagerBuilder允许您设置默认RedisCacheManager、事务行为和预定义缓存。
RedisCacheManager cacheManager RedisCacheManager.builder(connectionFactory).cacheDefaults(RedisCacheConfiguration.defaultCacheConfig()).transactionAware().withInitialCacheConfigurations(Collections.singletonMap(predefined,RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues())).build();
如前例所示RedisCacheManager允许在每个缓存的基础上进行自定义配置。 RedisCacheManager创建的RedisCaache的行为是用RedisCachConfiguration定义的。该配置允许您设置密钥过期时间、前缀和RedisSerializer实现以转换为二进制存储格式或从二进制存储格式转换如下例所示
RedisCacheConfiguration cacheConfiguration RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(1)).disableCachingNullValues();
RedisCacheManager默认为无锁RedisCachWriter用于读取和写入二进制值。无锁缓存提高了吞吐量。缺少条目锁定可能会导致Cache putIfAbsent和clean操作的重叠非原子命令因为这些操作需要向Redis发送多个命令。锁定对等方通过设置显式锁密钥并检查该密钥的存在来防止命令重叠这会导致额外的请求和潜在的命令等待时间。 锁定应用于缓存级别而不是每个缓存项。 可以选择以下锁定行为
RedisCacheManager cacheManager RedisCacheManager.build(RedisCacheWriter.lockingRedisCacheWriter(connectionFactory)).cacheDefaults(RedisCacheConfiguration.defaultCacheConfig())...
默认情况下缓存项的任何键都以实际缓存名称为前缀后跟两个冒号。此行为可以更改为静态前缀和计算前缀。 下面的示例演示如何设置静态前缀
// static key prefix
RedisCacheConfiguration.defaultCacheConfig().prefixCacheNameWith((͡° ᴥ ͡°));The following example shows how to set a computed prefix:// computed key prefix
RedisCacheConfiguration.defaultCacheConfig().computePrefixWith(cacheName - ¯\_(ツ)_/¯ cacheName);
缓存实现默认使用KEYS和DEL来清除缓存。键可能会导致大型键空间的性能问题。因此可以使用BatchStrategy创建默认RedisCacheWriter以切换到基于SCAN的批处理策略。SCAN策略需要批量大小以避免过多的Redis命令往返
RedisCacheManager cacheManager RedisCacheManager.build(RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory, BatchStrategies.scan(1000))).cacheDefaults(RedisCacheConfiguration.defaultCacheConfig())...
下表列出了RedisCacheManager的默认设置
Table 1. RedisCacheManager defaults SettingValue Cache Writer Non-locking, KEYS batch strategy Cache Configuration RedisCacheConfiguration#defaultConfiguration Initial Caches None Transaction Aware No
The following table lists the default settings for RedisCacheConfiguration:
Table 2. RedisCacheConfiguration defaults Key ExpirationNone Cache null Yes Prefix Keys Yes Default Prefix The actual cache name Key Serializer StringRedisSerializer Value Serializer JdkSerializationRedisSerializer Conversion Service DefaultFormattingConversionService with default cache key converters
1、Redis缓存过期
即使在不同的数据存储中空闲时间TTI和生存时间TTL的实现在定义和行为上也是不同的。 一般来说
生存时间TTL过期-TTL仅由创建或更新数据访问操作设置和重置。只要在TTL到期超时之前写入条目包括在创建时条目的超时将重置为配置的TTL过期超时持续时间。例如如果TTL过期超时设置为5分钟则在创建条目时超时将设置为5分并在此后和5分钟间隔到期之前更新条目时重置为5分钟。如果在5分钟内没有发生更新即使该条目被读取了几次或者甚至在5分钟间隔内只读取了一次该条目仍将过期。必须写入条目以防止在声明TTL过期策略时条目过期。空闲时间TTI过期-无论何时也读取条目以及条目更新TTI都会重置并且是TTL过期策略的有效扩展。 Time-To-Live (TTL)的过期
Spring Data Redis的缓存实现支持缓存项的生存时间TTL过期。用户可以通过提供新RedisCacheWriter的实现将TTL过期超时配置为固定的持续时间或者为每个缓存项配置动态计算的持续时间。TtlFunction接口。
如果所有缓存项都应在设置的持续时间后过期则只需将TTL过期超时配置为固定的持续时间如下所示
RedisCacheConfiguration fiveMinuteTtlExpirationDefaults RedisCacheConfiguration.defaultCacheConfig().enableTtl(Duration.ofMinutes(5));
然而如果TTL过期超时应该因缓存项而异则必须提供RedisCacheWriter的自定义实现。TtlFunction接口
enum MyCustomTtlFunction implements TtlFunction {INSTANCE;Overridepublic Duration getTimeToLive(Object key, Nullable Object value) {// compute a TTL expiration timeout (Duration) based on the cache entry key and/or value}
}
然后可以使用以下命令在全局基础上配置固定的持续时间或动态的每个缓存项持续时间TTL过期 全局固定持续时间TTL过期超时
RedisCacheManager cacheManager RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(fiveMinuteTtlExpirationDefaults).build();
或者或者 全局按缓存项动态计算持续时间TTL过期超时
RedisCacheConfiguration defaults RedisCacheConfiguration.defaultCacheConfig().entryTtl(MyCustomTtlFunction.INSTANCE);RedisCacheManager cacheManager RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(defaults).build();
当然您可以使用以下命令组合全局配置和每个缓存配置 全局固定持续时间TTL过期超时
RedisCacheConfiguration predefined RedisCacheConfiguration.defaultCacheConfig().entryTtl(MyCustomTtlFunction.INSTANCE);MapString, RedisCacheConfiguration initialCaches Collections.singletonMap(predefined, predefined);RedisCacheManager cacheManager RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(fiveMinuteTtlExpirationDefaults).withInitialCacheConfigurations(initialCaches).build();
Time-To-Idle (TTI)的过期
Redis本身不支持truetime-to-idleTTI过期的概念。尽管如此使用SpringDataRedis的Cache实现仍然可以实现类似于空闲时间TTI过期的行为。 必须显式启用Spring Data Redis缓存实现中的TTI配置即选择加入。此外您还必须使用固定的持续时间或自定义的TtlFunction接口实现来提供TTL配置如上面在Redis缓存过期中所述。 例如
Configuration
EnableCaching
class RedisConfiguration {BeanRedisConnectionFactory redisConnectionFactory() {// ...}BeanRedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {RedisCacheConfiguration defaults RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(5)).enableTimeToIdle();return RedisCacheManager.builder(connectionFactory).cacheDefaults(defaults).build();}
}
由于Redis服务器没有实现正确的TTI概念因此只有通过接受到期选项的Redis命令才能实现TTI。在Redis中“过期”从技术上讲是生存时间TTL策略。然而在读取密钥的值时可以传递TTL过期从而有效地重置TTL过期超时就像现在Spring Data Redis的Cache.getkey操作中的情况一样。 getkey通过调用Redis GETEX命令实现。