学习内容
redis缓存使用 autoloadcache
@Cache加缓存
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.jarvis.cache.annotation; import com.jarvis.cache.type.CacheOpType; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @Inherited @Documented public @interface Cache { int expire(); //缓存时间,过期时间 String expireExpression() default ""; //表达式,可与expire同时存在 int alarmTime() default 0; String key(); // 缓存的key值 String hfield() default ""; // 与key同时用时,key表示目录,hfield表示具体的key值 boolean autoload() default false; String autoloadCondition() default ""; long requestTimeout() default 36000L; String condition() default ""; CacheOpType opType() default CacheOpType.READ_WRITE; int waitTimeOut() default 500; ExCache[] exCache() default {@ExCache( expire = -1, key = "" )}; int lockExpire() default 10; boolean argumentsDeepcloneEnable() default true; }
例如:
@Cache(expire = 3600, key="upms:permission-autoCache:userrole",hfield = "#args[0]",expireExpression ="null==#retVal?1:3600")
// expire = 3600 ,表示设置key的过期时间为3600s。
// key="upms:permission-autoCache:userrole",hfield = "#args[0]" ,两者连用,key表示目录层级,hfield 表示具体的key键,#arg[0] 表示传入的第一个参数
// expireExpression ="null==#retVal?1:3600" ,#retVal表示方法的返回值,
//这个表达式的含义为,当返回值为null时,设置过期时间为1S,如果不是null,设置过期时间为3600S
@CacheDelete清除缓存
package com.jarvis.cache.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @Inherited @Documented public @interface CacheDelete { CacheDeleteKey[] value(); }
package com.jarvis.cache.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @Inherited @Documented public @interface CacheDeleteKey { String condition() default ""; String[] value();//key String hfield() default ""; }
举例:
@CacheDelete({@CacheDeleteKey(value= {"upms:permission-autoCache:permissions","upms:permission-autoCache:rolePermission","upms:permission-autoCache:permissions"})})
一般放在dao层。不能什么地方都有调接口的方法。