栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SqEL简单上手

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SqEL简单上手

sping 缓存

spEL(Spring Expression Language) 定义缓存key 和各种 condition 。

spring Cache 利用AOP代理结束来实现
spring 提供了 CacheManage ,可以配置redis 缓存。

注解作用参数说明
@Cacheable对方法配置,对方法结果缓存value: 缓存名 在spring配置文件中定义
key: 缓存的key ,可以为空或指定SpEL。默认按照方法所有参数进行组合
condition: 缓存条件,可以为空。SpEL 编写返回 true、fasle ,true时进行缓存
@CachePut方法配置,调用方法后对结果缓存参数同 @Cacheable 一样
@CacheEvict方法配置,按条件删除缓存参数痛@CAcheable 一样
allEntries: 是否清空所有缓存内容,默认fasle
beforeInvocation: 是否在方法执行前就清空,默认 fasle (方法出异常不会清空)
SpEL 介绍
@Cacheable(value = "users" , key = "#user.userName" , condition = "#user.age < 35")
User getUserList();
名称位置描述例子
methodNameroot对象当前被调用方法名#root.methodnaame
methodroot对象当前被调用方法#root.method.name
targetroot对象当前被调用的目标对象实例#root.target
targetClassroot对象当前被调用的目标对象的类#root.targetClass
argsroot对象当前被调用方法的参数列表#root.args[0]
cachesroot对象当前方法调用使用的缓存列表#root.caches[0].name
Argument执行上下文当前被调用的方法参数,
method( User u ) 通过 #u.name 获取
#u.name
result执行上下文方法执行后返回值( 方法执行后判定有效 )
@CacheVict (beforeInvocation = false)
#result
//接口上添加缓存
public interface CacheService {

    @Cacheable(value = "userList" , key = "methodName")
    List list();

    @Cacheable(value = "user",key = "'user'.concat(#id.toString())")
    User findUserById(Long id);

    //这里只修改 3 号
    //@CachePut(value="user",key="'user'.concat(#user.name.toString())")
    @CachePut(value="user",key="'user'.concat(3)")
    User update(User user);

    @CacheEvict(value = "user", key = "'user'.concat(#id.toString())")
    void remove(Long id);
}
底部
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/966845.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号