https://github.com/mchernyakov/various-ttl-map
Thread-safe map (cache) with various time to live of keys
https://github.com/mchernyakov/various-ttl-map
cache java map thread-safe-cache thread-safety ttl ttl-cache ttl-map
Last synced: about 1 year ago
JSON representation
Thread-safe map (cache) with various time to live of keys
- Host: GitHub
- URL: https://github.com/mchernyakov/various-ttl-map
- Owner: mchernyakov
- License: mit
- Created: 2019-12-22T23:13:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-01-07T20:23:23.000Z (over 4 years ago)
- Last Synced: 2025-03-22T23:04:00.895Z (about 1 year ago)
- Topics: cache, java, map, thread-safe-cache, thread-safety, ttl, ttl-cache, ttl-map
- Language: Java
- Homepage:
- Size: 108 KB
- Stars: 10
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Map with various time to live of keys
[](https://travis-ci.com/mchernyakov/various-ttl-map)
[](https://maven-badges.herokuapp.com/maven-central/com.github.mchernyakov/various-ttl-map/)
## Description
I's a cache (map) with various ttl of keys based on Redis [expire algorithm](https://redis.io/commands/expire)
and ConcurrentHashMap.
The implementation contains two maps:
* keys and values,
* keys and ttl.
And has two cleaning modes:
* `passive` via _get(K)_,
* `active` via _BackgroundCleaner_.
The BackgroundCleaner contains a thread pool, which is responsible for cleaning the map.
## Install
#### Maven
```xml
com.github.mchernyakov
various-ttl-map
0.0.3
```
#### Gradle
```groovy
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](https://redis.io/commands/expire#how-redis-expires-keys)),
`delayMillis`- interval between cleaning sessions (millis, default = 1000).
#### In code
```java
VariousTtlMap 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
- [ ] size of the cache,
- [ ] options for primitive map for ttl (several engines),
- [ ] async API,
- [ ] jmh tests.