Skip to content

Commit 232aebb

Browse files
committed
Created Version 1.0
1 parent 1bfa7f7 commit 232aebb

File tree

14 files changed

+583
-3
lines changed

14 files changed

+583
-3
lines changed

.gitattributes

Lines changed: 0 additions & 2 deletions
This file was deleted.

.gitignore

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.gradle
2+
build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
!**/src/main/**/build/
5+
!**/src/test/**/build/
6+
7+
### IntelliJ IDEA ###
8+
.idea
9+
.idea/modules.xml
10+
.idea/jarRepositories.xml
11+
.idea/compiler.xml
12+
.idea/libraries/
13+
*.iws
14+
*.iml
15+
*.ipr
16+
out/
17+
!**/src/main/**/out/
18+
!**/src/test/**/out/
19+
20+
### Eclipse ###
21+
.apt_generated
22+
.classpath
23+
.factorypath
24+
.project
25+
.settings
26+
.springBeans
27+
.sts4-cache
28+
bin/
29+
!**/src/main/**/bin/
30+
!**/src/test/**/bin/
31+
32+
### NetBeans ###
33+
/nbproject/private/
34+
/nbbuild/
35+
/dist/
36+
/nbdist/
37+
/.nb-gradle/
38+
39+
### VS Code ###
40+
.vscode/
41+
42+
### Mac OS ###
43+
.DS_Store

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,38 @@
11
# DanielsCache
2-
2+
A simple caching library for keeping a certain fixed amount of your computed objects with an LRU eviction strategy.
3+
4+
---
5+
### Time Complexity
6+
7+
| Complexity (usually) | Description |
8+
|----------------------|-------------------------|
9+
| `O(mapper function)` | Fetching unseen element |
10+
| `O(1)` | Fetching seen element |
11+
| `O(1)` | Eviction of LRU element |
12+
### How this works internally
13+
##### Fetching an unseen element
14+
- The result is computed and the value is stored in the `Hashmap` in a `new Node`
15+
- The element is added as `last`
16+
##### Fetching a seen element
17+
- The result is stored in the `Hashmap`, so it can be fetched and extracted from the `Node`
18+
- The element is removed from its current position and `prev` and `next` get linked together
19+
- The element is added as `last`
20+
##### Eviction of the LRU element
21+
- `first` points to the LRU element
22+
- `first.object` is removed from the `Hashmap`
23+
- `first.next` is the new `first`
24+
- `first.prev` gets unlinked
25+
##### Example of how it could look on the inside
26+
![img_1.png](img_1.png)
27+
### I want to use and/or adapt this project
28+
Go for it - I tried to make everything as readable and modifiable as possible. Check out the explanation below, and the license in the 'LICENSE' file. Regardless of the license, it would be cool if you somehow mentioned, that you got this code from here :)
29+
### How to use
30+
1. Add as library (here shown in IntelliJ) or add the code directly to your project
31+
![img_2.png](img_2.png)
32+
2. Create a new Cache instance with `DanielsCache<O, R> cache = new DanielsCache<>(int maxSize, Function<O, R> mapper);`
33+
- `O` is the input type e.g. a `String` from an incoming REST request
34+
- `R` is the computed type e.g. the object created from the REST string
35+
3. Use `cache.get(O object)` to get the corresponding `R result`
36+
- The result is computed if it is not in the cache
37+
- But simply fetched if it is
38+
- Eventually the cache fills up, and the LRU (least recently used) element is evicted

build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
build.dependsOn test
6+
7+
group = 'com.github.danielbinder'
8+
version = '1.0'
9+
10+
jar {
11+
archiveFileName.set('DanielsCache.jar')
12+
}
13+
14+
repositories {
15+
mavenCentral()
16+
}
17+
18+
dependencies {
19+
testImplementation platform('org.junit:junit-bom:5.9.1')
20+
testImplementation 'org.junit.jupiter:junit-jupiter'
21+
}
22+
23+
test {
24+
useJUnitPlatform()
25+
testLogging {
26+
events 'passed', 'skipped', 'failed'
27+
}
28+
}

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.gradle.console=plain

gradle/wrapper/gradle-wrapper.jar

59.3 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Sat Nov 25 13:47:25 CET 2023
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists

gradlew

Lines changed: 234 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)