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 @ Cacheable cache to solve the problem of double colons

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use @Cacheable cache to solve the problem of double colon", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to use @Cacheable cache to solve the problem of double colon"!

@Cacheable Cache Resolves Double Colons::

With spring-data-redis2.x,@Cacheable caches key values with double quotes after vlue or cacheNames by default

Modifications made through configuration to meet project requirements

@Configurationpublic class SpringCacheConfig{ @Bean public CacheManager cacheManager(LettuceConnectionFactory lettuceConnectionFactory){ RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofHour(1) //Change double colon to single colon .computePrefixWith(name - >":") .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.RedisCacheManagerBuilder .fromConnectionFactory(lettuceConnectionFactory) .cacheDefaults(config) .transactionAware() .build(); return cacheManager; } ... @ Cacheable(value = "PERSON",key = "#tagId+'_'+#zz")public List getPersonByTagid(Long tagId,String zz)

where value indicates which Cache the parameter returned by this method is stored in (i.e., the name of the cache block)

The cache result is stored in the cache as a key-value pair. In the annotation, key is the key of the key-value pair;value is the result returned by the method.

key in double quotes,#added to method parameters: get method parameters, '' means add delimiter between parameters

General usage

Use @EnableCaching//to enable caching on springboot's boot class

Use @Cacheable() directly on methods to use caching

At this point, I believe that everyone has a deeper understanding of "how to use @Cacheable cache to solve the problem of double colon," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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: 235

*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