首页 > 作文

springboot增加注解缓存@Cacheable的实现

更新时间:2023-04-04 05:02:43 阅读: 评论:0

目录
springboot增加注解缓存@cacheable业务层使用配置@cacheable注解的属性使用cachenames和valuekeykeygeneratorkeygeneratorconditionunless(除非)sync

springboot增加注解缓存@cacheable

业务层使用

@cacheable(value = "dictionary#1800", key = "#root.targetclass.simplename +':'+ #root.methodname +':'+ #code")    public object findbycode(string code) {        //业务    }

配置

import org.springframework.cache.cache;import org.springframework.data.redis.cache.rediscache;import org.springframework.data.redis.cache.rediscachemanager;import org.springframework.data.redis.core.redisoperations; import java.util.concurrent.concurrenthashmap;import java.util.concurrent.concurrentmap; public class myrediscachemanager extends rediscachemanager {      /**     * 过期时间分隔符     */    private static final string ttlparator = "#";    private final concurrentmap<string, cache> cachemap = new concurrenthashmap(16);    /**     * 过期时间, 单位为 秒     */    private long defaultexpiration = 0;      public myrediscachemanager(redisoperations redisoperations) {        super(redisoperations);    }      @override    public cache getcache(string name) {         long expiredtime = defaultexpiration;        if (name.contains(ttlparator)) {            string[] split = name.split(ttlparator);            string cachename = split[0];            try {                expiredtime = double.valueof(split[1]).longvalue();            } catch (exception e) {                e.printstacktrace();            }            cache cache = this.cachemap.get(name);            if (cache != null) {                return cache;            } el {                synchronized (this.cachemap) {                    cache = this.cachemap.get(name);                    if (cache == null) {                        cache = new rediscache(cachename, null, super.getredisoperations(), expiredtime);                        if (cache != null) {                            cache = this.decoratecache(cache);                            this.cachemap.put(name, cache);                        }                    }                     return cache;                }            }        }         return super.getcache(name);    }  }
import com.fasterxml.jackson.annotation.jsontypeinfo;import com.fasterxml.jackson.databind.objectmapper;import org.springframework.cache.cachemanager;import org.springframework.cache.annotation.cacheconfig;import org.springframework.cache.annotation.cachingconfigurersupport;import org.springframework.cache.annotation.enablecaching;import org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;import org.springframework.data.redis.core.redistemplate;import org.springframework.data.redis.rializer.genericjackson2jsonredisrializer;import org.springframework.data.redis.rializer.stringredisrializer; @cacheconfig@configuration@enablecachingpublic class redisconfig extends cachingconfigurersupport {     @bean    public cachemanager cachemanager(redistemplate redistemplate) {        //设置序列化key的实例化对象        redistemplate.tkeyrializer(new stringredisrializer());        //设置序列化value的实例化对象         objectmapper mapper = new objectmapper();        mapper.findandregistermodules();        mapper.enabledefaulttyping(objectmapper.defaulttyping.non_final, jsontypeinfo.as.property);        genericjackson2jsonredisrializer rializer = new genericjackson2jsonredisrializer(mapper);        redistemplate.tvaluerializer(rializer);        myrediscachemanager mrc = new myrediscachemanager(redistemplate);        return mrc;    } }

@cacheable注解的属性使用

cachenames和value

指定缓存组件的名字,通过下面代码可以看出可以将返回结果放在哪个缓存中,可以通过数组的方式指定多个缓存

 /**  * alias for {@link #cachenames}.  */ @aliasfor("cachenames") string[] value() default {}; /**  * names of the caches in which method invocation results are stored.  * <p>names may be ud to determine the target cache (or caches), matching  * the qualifier value or bean name of a specific bean definition.  * 现代音乐@since 4.2  * @e #value  * @e cacheconfig#cachenames  */ @aliasfor("value") string[] cachenames() default {};

key

缓存数据的时候使用的key,它是用来指定对应的缓存,模拟使用方法参数值作为key的值。也可以使用spel表达式的值来指定

 /**  * spring expression language (spel) expression for computing the key dynamically.  * <p>default is {@code ""}, meaning all method parameters are considered as a key,  * unless a custom {@link #keygenerator} has been configured.  * <p>the spel expression evaluates against a dedicated context that provides the  * following meta-data:  * <ul>  * <li>{@code #root.method}, {@code #root.target}, and {@code #root.caches} for  * references to the {@link java.lang.reflect.method method}, target object, and  * affected cache(s) respectively.</li>  * <li>shortcuts for the method name ({@code #root.methodname}) and target class  * ({@code #root.targetclass}) are also available.  * <li>method arguments can be accesd美声唱法 by index. for instance the cond argument  * can be accesd via {@code #root.args[1]}, {@code #p1} or {@code #a1}. arguments  * can also be accesd by name if that information is available.</li>  * </ul>  */ string key() default "";
名称位置描述示例methodnameroot object被调用的方法名称#root.methodnamemethodroot object被调用的方法#root.method.nametargetroot object当前被调用的目标对象#root.targettargetclassroot object当前被调用的目标对象类#root.targetclassargsroot object被调用方法的参数列表#root.args[0]cachesroot object调用的缓存列表#root.caches[0].nameargument nameevaluation context方法的参数名称可以直接使用#参数名#p0,#a0等等resultevaluation context执行方法后的返回值#result

可以通过这个参数提示列表看看到这个key所支持的root object对象有哪些,通过这样的方式可以指定对应的key值。

keygenerator

这个是表示指定的key的生成器,当然在之前分享中我们说过一个简单的key的生成策略。这里我们还可以通过自定的方式来实现这个key的生成策略。

keygenerator

这个是关于立冬的唯美句子表示指定的key的生成器,当然在之前分享中我们说过一个简单的key的生成策略。这里我们还可以通过自定的方式来实现这个key的生成策略。

import org.springframework.cache.interceptor.keygenerator;import org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;import java.lang.reflect.method;import java.util.arrays;@configurationpublic class mycacheconfig {    @bean("mykeygenerator")    public keygenerator keygenerator(){        return new keygenerator() {            @override            public object generate(object target, method method, object... params) {                return method.getname()+"["+ arrays.aslist(params).tostring()+"]";            }        };    }}

在使用的时候可以通过一下的方式进行配置

@cacheable(cachenames = {"emp"},keygenerator = "mykeygenerator")
cachemanager指定缓存管理器/**  * the bean name of the custom {@link org.springframework.cache.cachemanager} to u to  * create a default {@link org.springframework.cache.interceptor.cacheresolver} if none  * is t already.  * <p>mutually exclusive with the {@link #cacheresolver}  attribute.  * @e org.springframework.cache.interceptor.simplecacheresolver  * @e cacheconfig#cachemanager  */ string cachemanager() default ""; /**  * the bean name of the custom {@link org.springframework.cache.interceptor.cacheresolver}  * to u.  * @e cacheconfig#cacheresolver  */ string cacheresolver() default "";

condition

指定复合条件的情况下才缓存。也可以通过spel表达式进行设置。这个配置规则和上面表格中的配置规则是相同的。

 /**  * spring expression language (spel) expres编童话故事作文三年级sion ud for making the method  * caching conditional.  * <p>default is {@code ""}, meaning the method result is always cached.  * <p>the spel expression evaluates against a dedicated context that provides the  * following meta-data:  * <ul>  * <li>{@code #root.method}, {@code #root.target}, and {@code #root.caches} for  * references to the {@link java.lang.reflect.method method}, target object, and  * affected cache(s) respectively.</li>  * <li>shortcuts for the method name ({@code #root.methodname}) and target class  * ({@code #root.targetclass}) are also available.  * <li>method arguments can be accesd by index. for instance the cond argument  * can be accesd via {@code #root.args[1]}, {@code #p1} or {@code #a1}. arguments  * can also be accesd by name if that information is available.</li>  * </ul>  */ string condition() default "";

unless(除非)

当这个条件为true的时候,方法的返回值就不会被缓存。

/**  * spring expression language (spel) expression ud to veto method caching.  * <p>unlike {@link #condition}, this expres山西教育考试院官网sion is evaluated after the method  * has been called and can therefore refer to the {@code result}.  * <p>default is {@code ""}, meaning that caching is never vetoed.  * <p>the spel expression evaluates against a dedicated context that provides the  * following meta-data:  * <ul>  * <li>{@code #result} for a reference to the result of the method invocation. for  * supported wrappers such as {@code optional}, {@code #result} refers to the actual  * object, not the wrapper</li>  * <li>{@code #root.method}, {@code #root.target}, and {@code #root.caches} for  * references to the {@link java.lang.reflect.method method}, target object, and  * affected cache(s) respectively.</li>  * <li>shortcuts for the method name ({@code #root.methodname}) and target class  * ({@code #root.targetclass}) are also available.  * <li>method arguments can be accesd by index. for instance the cond argument  * can be accesd via {@code #root.args[1]}, {@code #p1} or {@code #a1}. arguments  * can also be accesd by name if that information is available.</li>  * </ul>  * @since 3.2  */ string unless() default "";

sync

是否异步

/**  * synchronize the invocation of the underlying method if veral threads are  * attempting to load a value for the same key. the synchronization leads to  * a couple of limitations:  * <ol>  * <li>{@link #unless()} is not supported</li>  * <li>only one cache may be specified</li>  * <li>no other cache-related operation can be combined</li>  * </ol>  * this is effectively a hint and the actual cache provider that you are  * using may not support it in a synchronized fashion. check your provider  * documentation for more details on the actual mantics.  * @since 4.3  * @e org.springframework.cache.cache#get(object, callable)  */ boolean sync() default fal;

注意

在使用这个属性的时候,当这个属性为true的时候,unless属性是不能使用的。

{@link #unless()} is not supported

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。

本文发布于:2023-04-04 05:02:42,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/71062a5fe4b689ca4a880f0012c8b2e8.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:springboot增加注解缓存@Cacheable的实现.doc

本文 PDF 下载地址:springboot增加注解缓存@Cacheable的实现.pdf

标签:缓存   可以通过   注解   方法
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图