Skip to content

Commit

Permalink
Add optimistic fast path for putIfAbsent to avoid locking
Browse files Browse the repository at this point in the history
This mirrors the optimization in computeIfAbsent, which only locks
if the lookup failed and the value may need to be computed.

See apache/openwhisk#2797 for a use-case
where the users had to modify their usage to obtain their
performance goals.
  • Loading branch information
ben-manes committed Mar 23, 2020
1 parent 8c397e2 commit 614fe60
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ Use Caffeine in a community provided integration:
* [ScalaCache][scala-cache]: Simple caching in Scala
* [Camel][camel]: Routing and mediation engine
* [JHipster][jhipster]: Generate, develop, deploy
* [Druid][druid]: Real-time analytics

Powering infrastructure near you:
* [Dropwizard][dropwizard]: Ops-friendly, high-performance, RESTful APIs
Expand All @@ -62,11 +61,12 @@ Powering infrastructure near you:
* [HBase][hbase]: A distributed, scalable, big data store
* [Apache Solr][solr]: Blazingly fast enterprise search
* [Infinispan][infinispan]: Distributed in-memory data grid
* [OpenWhisk][open-whisk]: Serverless cloud platform
* [Corfu][corfu]: A cluster consistency platform
* [Grails][grails]: Groovy-based web framework
* [Orbit][orbit]: Virtual actors on the JVM
* [Finagle][finagle]: Extensible RPC system
* [Neo4j][neo4j]: Graphs for Everyone
* [Druid][druid]: Real-time analytics

### In the News

Expand Down Expand Up @@ -130,7 +130,7 @@ Snapshots of the development version are available in
[finagle]: https://github.com/twitter/finagle
[druid]: https://druid.apache.org/docs/latest/configuration/index.html#cache-configuration
[jhipster]: https://www.jhipster.tech/
[orbit]: https://github.com/orbit/orbit
[open-whisk]: https://openwhisk.apache.org/
[camel]: https://github.com/apache/camel/blob/master/components/camel-caffeine/src/main/docs/caffeine-cache-component.adoc
[corfu]: https://github.com/CorfuDB/CorfuDB
[akka-http]: https://doc.akka.io/docs/akka-http/current/common/caching.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2006,6 +2006,17 @@ public Map<K, V> getAllPresent(Iterable<?> keys) {
return null;
}
}
} else if (onlyIfAbsent) {
// An optimistic fast path to avoid unnecessary locking
V currentValue = prior.getValue();
if ((currentValue != null) && !hasExpired(prior, now)) {
if (!isComputingAsync(prior)) {
tryExpireAfterRead(prior, key, currentValue, expiry(), now);
setAccessTime(prior, now);
}
afterRead(prior, now, /* recordHit */ false);
return currentValue;
}
}

V oldValue;
Expand Down
7 changes: 7 additions & 0 deletions checksum.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<trusted-key id='476634a4694e716a' group='com.googlecode.java-diff-utils' />
<trusted-key id='72475fd306b9cab7' group='com.googlecode.javaewah' />
<trusted-key id='80c08b1c29100955' group='com.jakewharton.fliptables' />
<trusted-key id='e0cb7823cfd00fbf' group='com.jakewharton.fliptables' />
<trusted-key id='a50569c7ca7fa1f0' group='com.jcraft' />
<trusted-key id='52931f4b72b4f54c' group='com.koloboke' />
<trusted-key id='1063fe98bcecb758' group='com.puppycrawl.tools' />
Expand Down Expand Up @@ -204,6 +205,9 @@
<dependency group='com.github.ben-manes' module='gradle-versions-plugin' version='0.27.0'>
<sha512>EEA3D1118C6DE8F890AB6A04EA3505C149F99C034C26707C44ED736F6603C6253A03BA07D0361C4D1008E5CE944B0DD7457DC1AF2CD6B01091F6594261DB1F77</sha512>
</dependency>
<dependency group='com.github.ben-manes' module='gradle-versions-plugin' version='0.28.0'>
<sha512>6B6E1C845DF98A6EFD07354F7DC2FABB77FFB171F20BE88BC232270AEB0243370B5265E2A310D8CBAC77F83F2B0D11AE8EB59A9ED1F030D5789357DA3899798B</sha512>
</dependency>
<dependency group='com.github.brianfrankcooper.YCSB' module='core' version='0.15.0'>
<sha512>DF5BFED7DFC327166FE0CB81BD6A9655F44CCB268E7E69204BD09955147CE9A97EBBCC28F9ACF315DBAF49E850A43CE8744F44E68D7BE816EACCAF0BEE41B203</sha512>
</dependency>
Expand Down Expand Up @@ -342,6 +346,9 @@
<dependency group='org.testng' module='testng' version='7.1.1'>
<sha512>508B1F1594A3E377DD9AEBA9EF1EF43DA24F22BE488B4AF9EDB719D02814741B47AD69D22A7E8CAF799D847A5CE20DE8B199586958AECA17C79EFDCA8D257E</sha512>
</dependency>
<dependency group='org.testng' module='testng' version='7.2.0'>
<sha512>AC7BBD700CB88EDB3E0FC92D7A0D659F42C97E6E0B5E21DD3EC2EC8537841CAEA50E99AC3414026BFE54D89EA72F6F635F964B429EC8A1E5BF6F44101AC58868</sha512>
</dependency>
<dependency group='xerces' module='xercesImpl' version='2.9.1'>
<sha512>37A13B129F3536A53F2A553151A53997DA6DE7CE4D7231EFEEFD26A68C92BE309666F2EE1F527D3B8C38BC6ADDC9FCCBBDD0D134759FD88667976B0CFF842435</sha512>
</dependency>
Expand Down
24 changes: 12 additions & 12 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@
*/
ext {
versions = [
akka: '2.6.3',
akka: '2.6.4',
cache2k: '1.3.1.Alpha',
checkerFramework: '3.1.0',
checkerFramework: '3.2.0',
collision: '0.3.3',
commonsCompress: '1.19',
commonsCompress: '1.20',
commonsLang3: '3.9',
commonsMath3: '3.6.1',
concurrentlinkedhashmap: '1.4.2',
config: '1.4.0',
ehcache3: '3.8.1',
errorprone: '2.3.4',
errorproneJavac: '9+181-r4173-1',
elasticSearch: '7.5.2',
elasticSearch: '7.6.1',
expiringMap: '0.5.9',
fastfilter: 'bf0b02297f',
fastutil: '8.3.0',
flipTables: '1.0.2',
fastutil: '8.3.1',
flipTables: '1.1.0',
guava: '28.2-jre',
jackrabbit: '1.24.0',
jamm: '0.3.3',
Expand All @@ -68,30 +68,30 @@ ext {
jcacheTck: '1.1.1',
jctools: '3.0.0',
junit: '4.13',
mockito: '3.2.4',
mockito: '3.3.3',
paxExam: '4.13.1',
testng: '7.1.1',
testng: '7.2.0',
truth: '0.24',
]
pluginVersions = [
apt: '0.21',
bnd: '5.0.0',
checkstyle: '8.29',
checkstyle: '8.30',
coveralls: '2.8.4',
coverity: '1.0.10',
errorprone: '1.1.1',
jacoco: '0.8.4',
jmh: '0.5.0',
jmhReport: '0.9.0',
nullaway: '1.0.1',
pmd: '6.21.0',
pmd: '6.22.0',
semanticVersioning: '1.1.0',
shadow: '5.2.0',
sonarqube: '2.8.0.1969',
spotbugs: '4.0.0-RC1',
spotbugsPlugin: '3.0.0',
stats: '0.2.2',
versions: '0.27.0',
versions: '0.28.0',
]

libraries = [
Expand Down Expand Up @@ -166,7 +166,7 @@ ext {
exclude group: 'junit'
exclude group: 'guice'
},
'com.google.inject:guice:4.2.2'
'com.google.inject:guice:4.2.3'
],
truth: "com.google.truth:truth:${testVersions.truth}",
]
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-rc-4-bin.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down

0 comments on commit 614fe60

Please sign in to comment.