Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the method of spring caching custom resolver

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use spring cache custom resolver". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Overview

Cache-aside mode is a commonly used cache usage mode.

The usage process is shown below:

When the data in the database is updated, the cache is invalidated, and the latest data in the database can be read later, making the cached data consistent with the database data.

Cache processing is carried out through cache annotations in spring, and cache processing is generally encapsulated into the dao layer, so that the business layer does not need to be aware of the details of the cache operation and can focus on the processing of business logic.

II. Cache reading and invalidation

The operation of the dao layer usually uses springdatajpa, and the database method is an interface, which is cached by adding corresponding cache annotations to the interface.

Read data:

@ Cacheable (value = "testCache", key = "# p0", unless = "# result = = null") Optional findById (Long id)

With Cacheable annotations, after data is read from the database, it is synchronously written to the cache.

Save the data:

@ CacheEvict (value = "testCache", key = "# p0.id") DemoEntity save (DemoEntity entity)

With CacheEvict annotations, the cache is invalidated after the data is written to the database.

What if we want to do something else after the cache expires, such as writing the invalidated key to kafka for other systems to delete the cache synchronously?

3. Custom cache resolver

Spring provides a way to customize the cache resolver. By customizing the resolver, you can add additional operations to the cache processing.

@ Configurationpublic class RedisCacheConfig extends CachingConfigurerSupport {@ Bean public RedisCacheManager redisCacheManager (RedisConnectionFactory redisConnectionFactory) {RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig () .computePrefixWith (cacheName-> cacheName.concat (":")); return RedisCacheManager.builder (redisConnectionFactory) .cacheDefaults (cacheConfiguration) .build ();} @ Bean public CacheResolver customCacheResolver (RedisConnectionFactory redisConnectionFactory) {return new CustomCacheResolver (redisCacheManager (redisConnectionFactory));}}

The above code is the configuration of redis cache, where the RedisCacheManager part is the configuration of the regular cacheManager, and the customCacheResolver part is the configuration of the custom resolver. By defining the bean of customCacheResolver, you can refer to this custom resolver in the cache comments.

Once the bean of customCacheResolver is defined, we can refer to the modified code of the data preservation method mentioned above in the cache comments:

@ CacheEvict (value = "testCache", cacheResolver = "customCacheResolver", key = "# p0.id") DemoEntity save (DemoEntity entity)

The specified cacheResolver is added to CacheEvict compared to the previous implementation.

Fourth, the implementation of custom resolver.

We described above that if you configure and reference cacheResolver, the following describes the implementation of a custom cacheResolver.

Public class CustomCacheResolver extends SimpleCacheResolver {public CustomCacheResolver (CacheManager cacheManager) {super (cacheManager);} @ Override @ NonNull public Collection

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report