Giter Site home page Giter Site logo

cache-example's Introduction

Simple cache example

This is an example for a simple in memory concurrent cache.

Usage

ExpireCache<String, Integer> cache = new ExpireCache<>();
cache.put("a", 1);

int a = cache.get("a");

// remove "a" from cache
cache.delete("a");

// set a key that will expire in 1 second
cache.put("b", 2, 1, TimeUnit.SECONDS);

int b = cache.get("b");

// cleanout any resources used by cache.
cache.close();

Thread safety

Each instance of ExpireCache is threadsafe.

Enhancements left out

  • Monitoring metrics
  • Expire on write, on read settings

Performance

The expire cache performance is dependant on its storage.
The storage used here is the Java ConcurrentHasMap, which as a hashmap has a constant lookup time O(1)

The default expire thread does a linear search over all cache instances in O(n).
This means if we have 2 cache instances of each 5K entries, the expire thread will go over all 10K entries one at a time. In practice for a cache where accurate cache expires are not required this should not be an issue.

JMH Benchmarks:

Benchmark                                           Mode     Cnt   Score    Error  Units
BenchmarkTests.benchmarkGet                       sample  117612  ≈ 10⁻⁴           ms/op
BenchmarkTests.benchmarkGet:benchmarkGet·p0.00    sample          ≈ 10⁻⁶           ms/op
BenchmarkTests.benchmarkGet:benchmarkGet·p0.50    sample          ≈ 10⁻⁴           ms/op
BenchmarkTests.benchmarkGet:benchmarkGet·p0.90    sample          ≈ 10⁻⁴           ms/op
BenchmarkTests.benchmarkGet:benchmarkGet·p0.95    sample          ≈ 10⁻⁴           ms/op
BenchmarkTests.benchmarkGet:benchmarkGet·p0.99    sample          ≈ 10⁻³           ms/op
BenchmarkTests.benchmarkGet:benchmarkGet·p0.999   sample           0.007           ms/op
BenchmarkTests.benchmarkGet:benchmarkGet·p0.9999  sample           0.029           ms/op
BenchmarkTests.benchmarkGet:benchmarkGet·p1.00    sample           0.695           ms/op
BenchmarkTests.benchmarkPut                       sample   91054  ≈ 10⁻⁴           ms/op
BenchmarkTests.benchmarkPut:benchmarkPut·p0.00    sample          ≈ 10⁻⁵           ms/op
BenchmarkTests.benchmarkPut:benchmarkPut·p0.50    sample          ≈ 10⁻⁴           ms/op
BenchmarkTests.benchmarkPut:benchmarkPut·p0.90    sample          ≈ 10⁻⁴           ms/op
BenchmarkTests.benchmarkPut:benchmarkPut·p0.95    sample          ≈ 10⁻⁴           ms/op
BenchmarkTests.benchmarkPut:benchmarkPut·p0.99    sample          ≈ 10⁻³           ms/op
BenchmarkTests.benchmarkPut:benchmarkPut·p0.999   sample           0.013           ms/op
BenchmarkTests.benchmarkPut:benchmarkPut·p0.9999  sample           0.020           ms/op
BenchmarkTests.benchmarkPut:benchmarkPut·p1.00    sample           0.049           ms/op

cache-example's People

Contributors

gerritjvv avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.