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 add a constant to Cacheable's key

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

Share

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

This article mainly introduces how to add constants to Cacheable's key, which is very detailed and has a certain reference value. Interested friends must finish reading it!

Key of Cacheable plus constant background   

Because cached annotations have similar prefixes, constants are extracted.

Public static final String KEY_CERT_TYPE_CODE_PREFIX = "ec_cert_type:cert_type_code:"; @ Override@Cacheable (key = KEY_CERT_TYPE_CODE_PREFIX + "+ # code") public EcCertType loadCertTypeByCode (String code) {/ / something return type;}

However, when in use, an error is reported:

EL1041E: After parsing a valid expression, there is still more data in the expression

After modification, add single quotation marks to the constant so that it will not be parsed by SpEL and solved.

Public static final String KEY_CERT_TYPE_CODE_PREFIX = "'ec_cert_type:cert_type_code:'"; @ Override@Cacheable (key = KEY_CERT_TYPE_CODE_PREFIX + "+ # code") public EcCertType loadCertTypeByCode (String code) {/ / something return type;} springcache key. Do not specify key, use the default key generator

With the default key generator, the cached object needs to implement the toString () method, stitching the fields you want to use as key.

After calling the query API, the key in the cache is as follows:

Where TRANSFORMERS-ACT_DICT-KEY is the defined value value, indicating which cache it is placed in.

two。 Specify key value

After running, the key value is as follows

At this point, the id value in the passed Student object is used as the key.

3. Custom key Generator

Reference a custom generator

The key values are as follows:

The above is all the contents of the article "how to add constants to the key of Cacheable". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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