首页 > 数据库 >redis:清空 spring boot注解式

redis:清空 spring boot注解式

时间:2023-04-21 17:34:44浏览次数:44  
标签:userName 缓存 spring boot redis accountCache value key true


flushall 清空

打开D:\Program Files\Java\Redis-x64-3.2.100\redis-cli.exe

auth 123456
flushall

 

 

docker

docker exec -it   65e343434e6e  redis-cli
auth 123

flushall  

exit

 

@Cacheable  :根据方法的请求参数对其结果进行缓存

参数

解释

example

value

缓存的名称,在 spring 配置文件中定义,必须指定至少一个

例如:

@Cacheable(value=”mycache”)

@Cacheable(value={”cache1”,”cache2”}

key

缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合

@Cacheable(value=”testcache”,key=”#userName”)

condition

缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存

@Cacheable(value=”testcache”,condition=”#userName.length()>2”)

参考

//@Cacheable(value=”accountCache”),这个注释的意思是,当调用这个
方法的时候,会从一个名叫 accountCache 的缓存中查询,如果没有,则执行实际的
方法(即查询数据库),
    //并将执行的结果存入缓存中,否则返回缓存中的对象。这里的缓存中的 key 就
是参数 userName,value 就是 Account 对象。“accountCache”缓存是在 spring*.xml 
中定义的名称。
    @Cacheable(value="accountCache")
    public Account getAccountByName(String userName) {
        // 方法内部实现不考虑缓存逻辑,直接实现业务
        System.out.println("real query account."+userName);
        return getFromDB(userName);
    }

 

@CachePut:  的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用

参数

解释

example

value

缓存的名称,在 spring 配置文件中定义,必须指定至少一个

@CachePut(value=”my cache”)

key

缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合

@CachePut(value=”testcache”,key=”#userName”)

condition

缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存

@CachePut(value=”testcache”,condition=”#userName.length()>2”)

 

//@CachePut 注释,这个注释可以确保方法被执行,同时方法的返回值也被记录到缓存中,
//实现缓存与数据库的同步更新。
 
@CachePut(value="accountCache",key="#account.getName()")// 更新accountCache 缓存
public Account updateAccount(Account account) { 
  return updateDB(account); 
}

@CachEvict  :  的作用 主要针对方法配置,能够根据一定的条件对缓存进行清空  

参数

解释

example

value

缓存的名称,在 spring 配置文件中定义,必须指定至少一个

@CacheEvict(value=”my cache”)

key

缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则缺省按照方法的所有参数进行组合

@CacheEvict(value=”testcache”,key=”#userName”)

condition

缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存

@CacheEvict(value=”testcache”,condition=”#userName.length()>2”)

allEntries

是否清空所有缓存内容,缺省为 false,如果指定为 true,则方法调用后将立即清空所有缓存

@CachEvict(value=”testcache”,allEntries=true)

beforeInvocation

是否在方法执行前就清空,缺省为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存

@CachEvict(value=”testcache”,beforeInvocation=true)

 

//@CacheEvict(value="accountCache",key="#account.getName()")
// 清空accountCache 缓存 
public void updateAccount(Account account) {
   updateDB(account); 
} 
 
//@CacheEvict(value="accountCache",allEntries=true)
// 清空accountCache 缓存
public void reload() {
   reloadAll()
}
 
//@Cacheable(value="accountCache",condition="#userName.length() <=4")
// 缓存名叫 accountCache 
public Account getAccountByName(String userName) { 
 // 方法内部实现不考虑缓存逻辑,直接实现业务
 return getFromDB(userName); 
}

 

标签:userName,缓存,spring,boot,redis,accountCache,value,key,true
From: https://blog.51cto.com/u_16082902/6213851

相关文章

  • Redis-Cluster(redis集群)
    Redis-Cluster(redis集群)Redis-Cluster的背景介绍1.1存在的问题1.并发量:单机Redisqps为10w/s,但是我们需要百万级别的并发量2.数据量:机器内存16-256g,如果存储500g数据呢1.2解决#解决方法:加机器,分布式rediscluster在15年加入了,满足了分布式的需求数据发布(分布式数据......
  • Spring cloud:分布式module
     新增一个业务模块,统一将业务模块放在hc-modeules下在resources资源目录下新增application-properties.yml文件 该文件用来配置注册中心、配置中心信息等..在pom.xml文件添加jar包的依赖以及配置指向的父级<parent> <groupId>com.github.pig</groupId> <artifactId>pig-modu......
  • SpringCloud中使用Apollo实现动态刷新
    SpringSpringBootSpringCloud中使用Apollo实现动态刷新普通字段在需要刷新的字段上使用@value注解即可,例如:@Value("${test.user.name}")privateStringname;@Value("${test.user.age}")privateIntegerage;@Value("${test.user.sex}")......
  • springboot框架快速整合websocket
    1、【pom.xml】<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId></dependency>2、【MsgType.java】/***@authorJHL*2019-08-109:56*/publicenumM......
  • redis2
    1哈希类型###!---hget,hset,hdelhgetkeyfield#获取hashkey对应的field的value时间复杂度为o(1)hsetkeyfieldvalue#设置hashkey对应的field的value值时间复杂度为o(1)hdelkeyfield#删除hashkey对应的field的值时间复杂度为o(1)#测试hsetuser:1:in......
  • redis的key命名规范
    一、键值设计1.key名设计【建议】:可读性和可管理性以业务名(或数据库名)为前缀(防止key冲突),用冒号分隔,比如业务名:表名:idredis使用的时候注意命名空间,一个项目一个命名空间,项目内业务不同命名空间也不同。一般情况下:1)第一段放置项目名或缩写如project2)第二段把表名......
  • spring boot项目上传文件
    严重:Servlet.service()forservlet[dispatcherServlet]incontextwithpath[]threwexception[Requestprocessingfailed;nestedexceptionisorg.springframework.web.multipart.MaxUploadSizeExceededException:Maximumuploadsizeexceeded;nestedexception......
  • 数据类型和SpringMvc
    1.Java的八种数据类型和各自取值范围?byte 1      float 4short 2     double 8int 4       boolean true/falselong 8      char  2 2.String属于基本类型吗?String的常用API?string属于Java中的字符串类型,也是一个引用类型,并不属......
  • 【汇智学堂】微服务-SpringBoot环境搭建之一maven下载安装
    一、下载maven3.6.2http://maven.apache.org/download.cgi下载完成后,解压到某个目录。本人目录如下。(C:\Users\leilei\Desktop\apache-maven-3.6.2-bin\apache-maven-3.6.2)二、配置环境变量系统环境变量里,添加MAVEN_HOME(或M2_HOME),其值为C:\Users\leilei\Desktop\apache-maven......
  • 【汇智学堂】单机部署使用Redis
    First:https://github.com/microsoftarchive/redis/releasesDownload,unzip,asthis:Second,runcmd,startredisserviceredis-server.exeredis.windows.confAsabove,serviceissuccess。Thisisserver,ifclosethiswindow,servicewillbeclosed.ThirdAnotherc......