The Wayback Machine - http://web.archive.org/web/20200917073634/https://github.com/mchernyakov/various-ttl-map
Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Map with various time to live of keys

Build Status Maven Central

Description

It is cache (map) with various ttl of keys based on Redis expire algorithm and ConcurrentHashMap.

The implementation contains two maps:

  1. keys and values,
  2. keys and ttl.

And has two variants of cleaning:

  1. passive via get(K),
  2. active via BackgroundCleaner.

The BackgroundCleaner contains thread pool which is responsible for cleaning map.

Install

Maven

<dependency>
  <groupId>com.github.mchernyakov</groupId>
  <artifactId>various-ttl-map</artifactId>
  <version>0.0.3</version>
</dependency>

Gradle

compile group: 'com.github.mchernyakov', name: 'various-ttl-map', version: '0.0.3'

Usage

Properties

Builder properties:

defaultTtl - default ttl (seconds),

cleaningPoolSize - cleaning pool size (default = 1),

numCleaningAttemptsPerSession - how many attempts cleaner can do in single session,

waterMarkPercent - percent when the cleaner has to start another session (basically it means that we have a lot of expired keys, see algo),

delayMillis- interval between cleaning sessions (millis, default = 1000).

In code

    VariousTtlMap<String, String> map = VariousTtlMapImpl.Builder.newBuilder()
            .setDefaultTtl(2)
            .setCleaningPoolSize(2)
            .setNumCleaningAttemptsPerSession(250)
            .setWaterMarkPercent(10)
            .setDelayMillis(100)
            .build();

    int attempts = 10_000;
    Random random = new Random();
        for (int i = 0; i < attempts; i++) {
        map.put("key_" + random.nextInt(), "val", random.nextInt(5));
    }

Roadmap

  • options for primitive map for ttl (several engines),
  • async API,
  • jmh tests.
You can’t perform that action at this time.