@Cacheable(value="bookCache", key="isbn")

public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)

How can I specify @Cachable to use isbn and checkWarehouse as key?

解决方案

Update: Current Spring cache implementation uses all method parameters as the cache key if not specified otherwise. If you want to use selected keys, refer to Arjan's answer which uses SpEL list {#isbn, #includeUsed} which is the simplest way to create unique keys.

The default key generation strategy changed with the release of Spring

4.0. Earlier versions of Spring used a key generation strategy that, for multiple key parameters, only considered the hashCode() of

parameters and not equals(); this could cause unexpected key

collisions (see SPR-10237 for background). The new

'SimpleKeyGenerator' uses a compound key for such scenarios.

Before Spring 4.0

I suggest you to concat the values of the parameters in Spel expression with something like key="#checkWarehouse.toString() + #isbn.toString()"), I believe this should work as org.springframework.cache.interceptor.ExpressionEvaluator returns Object, which is later used as the key so you don't have to provide an int in your SPEL expression.

As for the hash code with a high collision probability - you can't use it as the key.

Someone in this thread has suggested to use T(java.util.Objects).hash(#p0,#p1, #p2) but it WILL NOT WORK and this approach is easy to break, for example I've used the data from SPR-9377 :

System.out.println( Objects.hash("someisbn", new Integer(109), new Integer(434)));

System.out.println( Objects.hash("someisbn", new Integer(110), new Integer(403)));

Both lines print -636517714 on my environment.

P.S. Actually in the reference documentation we have

@Cacheable(value="books", key="T(someType).hash(#isbn)")

public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)

I think that this example is WRONG and misleading and should be removed from the documentation, as the keys should be unique.

P.P.S. also see https://jira.springsource.org/browse/SPR-9036 for some interesting ideas regarding the default key generation.

I'd like to add for the sake of correctness and as an entertaining fact that using a secure cryptographic hash function like SHA256, due to the properties of such function IS possible for this task, but to compute it every time may be too expensive.

cacheable 表达式,多个方法参数的@Cacheable键相关推荐

  1. cacheable 表达式_Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用

    从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...

  2. cacheable 表达式_springboot @Cacheable 基本使用

    加入依赖: org.springframework.boot spring-boot-starter-cache 开启注解缓存: 在启动类上加入 @EnableCaching 缓存注解 @Cachea ...

  3. cacheable 表达式_Spring Boot缓存注解@Cacheable、@CacheEvict、@CachePut使用

    从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...

  4. 委托到Lambda的进化: ()= {} 这个lambda表达式就是一个无参数的委托及具体方法的组合体。...

    1.原始的委托 (.net 1.0) using System; using System.Collections.Generic; using System.ComponentModel; usin ...

  5. 方法参数名称和Spring

    继续之前的博客文章有关构造函数和方法参数以及Java在运行时不保留参数名称的情况–先前的文章涉及构造函数不保留参数名称及其对Spring中的Contructor注入的含义,在此我将介绍更多内容不保留参 ...

  6. C#参考之方法参数关键字:params、ref及out

    params 使用 params 关键字可以指定采用数目可变的参数的方法参数. 可以发送参数声明中所指定类型的逗号分隔的参数列表或指定类型的参数数组. 还可以不发送参数. 如果未发送任何参数,则 pa ...

  7. python3.5怎么使用-Python3.5常见内置方法参数用法实例详解

    本文实例讲述了Python3.5常见内置方法参数用法.分享给大家供大家参考,具体如下: Python的内置方法参数详解网站为:https://docs.python.org/3/library/fun ...

  8. python lambda表达式的使用方法(匿名函数)

    文章目录 lambda函数简介 示例1:将存有文件名的字符串列表,运用正则表达式方法提取文件名中的数字,按照数字大小将文件名字符串列表重新排序 示例2:将存有hsv颜色空间的颜色元组列表,转换成rgb ...

  9. Lambda表达式替代匿名方法

    使用Lambda表达式,"=>"之前为参数列表,如果有多个参数,则不能省略括号,比如:(s,e)=>.... 如果方法有返回值,并且处理代码只有一行,可以简写成i=&g ...

最新文章

  1. 一张图看懂项目管理的47个过程
  2. StringUtils.join的用法
  3. SSM中进行Junit单元测试时无法注入service
  4. 李超:WebRTC传输与服务质量
  5. 超1亿人选择朋友圈三天可见,背后的原因值得深思
  6. 《学习之道》第十章方法空间能力是可以后天形成的
  7. mysql去掉两个最高分_MySQL中查询、删除重复记录的方法大全
  8. Android SeekBar和RatingBar示例教程
  9. /dev/mapper/centos-root 100% 虚拟机硬盘空间占满
  10. ar机房查看html5,探秘5G“智慧机房”:5G+AR技术实现机房智能巡检
  11. 利用ST-LINK配合ST-LINK Utility 将bin文件下载到STM32的FLASH中
  12. 几种常见的7号电池的容量
  13. b和kb的换算_b和bit换算(KB转换G)
  14. SUSE常见问题解决办法
  15. python爬虫 爬取小姐姐图片
  16. xml解析方式与效率对比
  17. Java程序:jstack
  18. GIS开发之二维地下管线综合管理系统(Arcgis)第三节 使用python和modelbuilder
  19. 孕妇不能吃哪些食物?(协和妇产权威版)
  20. 如何查看IE浏览器保存的密码

热门文章

  1. Oracle根据生日计算年龄
  2. 中英文标点符号切换的组合键_电脑键盘常用组合键(快捷键)之——Ctrl键组合...
  3. vlan的几种划分方式
  4. candidate expects 1 argument, 0 provided 错误解决
  5. 市场营销问题 (二):产品属性的效用函数
  6. 错误: 找不到或无法加载主类的解决办法
  7. 研究生毕业后工资高多少?
  8. 使用DirectSound播放MP3文件
  9. MallBook分账系统收费标准是什么呢?
  10. 三个角度细谈:如何发挥朋友圈广告的威力