Redis Java framework Distributed Cache

Why use a distributed cache in Java application?

To improve program performance and speed applications, every millisecond counts. According to a study Google, if a Web site for three seconds or less did not load successfully, there will be 53% of mobile phone users will leave.

Cache is to allow distributed applications to accelerate one of the important technologies. Information stored closer to CPU, faster access. Loading data from the CPU cache is much faster than loading from the RAM, the ratio of loading from the hard disk or a network much faster.

To store frequently accessed data, distributed applications need to maintain a cache on multiple machines. Distributed caching to reduce latency distributed applications to improve concurrency and scalability of an important strategy.

Redis is a popular open source data storage memory, can be used as a database, a cache or a message broker. Since the data is loaded from memory rather than disk, Redis than many traditional database solutions to market faster.

However, the developer is working properly distributed cache so that Redis is a huge challenge. For example, care must be taken locally cache invalidation that replace or delete the cache entry. Each time you update or delete information stored in computers in the local cache, the cache must update all the computers in a distributed memory caching system.

The good news is that there are a number of similar frameworks such as Redis Redisson, can help build the required applications distributed cache. The next section discusses three important realization Redisson in distributed cache: Maps, Spring Cache and JCache.

1. Redisson Distributed Cache

Redisson is based on the Redis frame, with a Java implementation Redis wrapper (warpper) and an interface. Redisson contains many common Java classes, such as distributed objects, distributed services, distributed locking and synchronization, as well as distributed collection. As described immediately below, some interfaces to support distributed caching and local caching.

2. Map

Map is one of the most useful collection of Java. Redisson provides a Java Map named RMap implement, and support the local cache.

If you want to perform multiple read operations or network loopback (roundtrip), should be used RMap support local cache. Map data is stored by local, RMap 45 times faster than non-enable the local cache. Universal distributed cache use RMapCache, local cache using RLocalCachedMap.

Redis engine itself is capable of performing the cache, the client does not need to execute code. However, although the local cache can significantly improve reading speed, but need to be maintained by the developer, and may need some development work. Redisson provides RLocalCachedMap target for developers to make it easier to implement the local cache.

The following code shows how to initialize RMapCache objects:

RMapCache<String, SomeObject> map = redisson.getMapCache("anyMap"); 
map.put("key1", new SomeObject(), 10, TimeUnit.MINUTES, 10, TimeUnit.SECONDS); 
复制代码

The above code string "key1" RMapCache put in, and () associated with SomeObject. It then specifies two parameters, TTL is set to 10 minutes and a maximum idle time of 10 seconds.

When no longer needed, it should be destroyed RMapCache objects:

map.destroy(); 
复制代码

After the close Redisson do not destroy operation.

3. Spring Cache

Spring is a Java framework for building enterprise-class Web applications, but also provides caching support.

Redisson includes Spring caching feature, two objects: RedissonSpringCacheManager and RedissonSpringLocalCachedCacheManager. RedissonSpringLocalCachedCacheManager support local cache.

Here is a

RedissonSpringLocalCachedCacheManager exemplary configuration objects:

@Configuration 
@ComponentScan 
@EnableCaching 
public static class Application { 
    @Bean(destroyMethod="shutdown") 
    RedissonClient redisson() throws IOException { 
        Config config = new Config(); 
        config.useClusterServers() 
                .addNodeAddress("127.0.0.1:7004", "127.0.0.1:7001"); 
        return Redisson.create(config); 
    } 
    @Bean 
    CacheManager cacheManager(RedissonClient redissonClient) { 
        Map<String, CacheConfig> config = new HashMap<String, CacheConfig>(); 
        // 新建 "testMap" 缓存:ttl=24分钟,maxIdleTime=12分钟 
        config.put("testMap", new CacheConfig(24*60*1000, 12*60*1000)); 
        return new RedissonSpringCacheManager(redissonClient, config); 
    } 
} 
复制代码

Further, it also reads the configuration file JSON or YAML RedissonSpringCacheManager.

And RMaps the same,

RedissonSpringCacheManager Each instance has two important parameters: ttl (time to live) and maxIdleTime. If these parameters are not defined or set to 0, then the data will remain in the cache indefinitely.

4. JCache

JCache is a Java caching API, allowing developers to cache from temporary storage, retrieval, update, and delete objects.

Redisson provides Redis of JCache API implementation. Here is an example of the default configuration JCache API calls in Redisson in:

MutableConfiguration<String, String> config = new MutableConfiguration<>(); 
CacheManager manager = Caching.getCachingProvider().getCacheManager(); 
Cache<String, String> cache = manager.createCache("namedCache", config); 
复制代码

In addition, use can also be custom configuration file, Redisson Config object or objects arranged Redisson RedissonClient JCache. For example, the following code is used to call a custom configuration Redisson JCache:

MutableConfiguration<String, String> jcacheConfig = new MutableConfiguration<>(); 
Config redissonCfg = ... 
Configuration<String, String> config = RedissonConfiguration.fromConfig(redissonCfg, jcacheConfig); 
CacheManager manager = Caching.getCachingProvider().getCacheManager(); 
Cache<String, String> cache = manager.createCache("namedCache", config); 
复制代码

JCache achieve Redisson has passed all tests of JCache TCK. You can also independently verified.


Guess you like

Origin juejin.im/post/5d1e00335188257cbb477277