| Primary Author | Chris Shorrock |
|---|---|
| Stable Version | N/A |
| Snapshot Version | 1.0-SNAPSHOT |
| Scala Version | 2.8.0 |
A really simple map cache implementation in scala, which has rule based expiration of elements, functional constructs and cache stampede protection. Very simplistic, use it if you like. Just threw it in github so that I didn’t have to re-implement logic across projects.
Usage is quite simple. Create an instance of the cache. Pass in a request to retrieve contents with the function needed to populate on cache expiration.
import com.shorrockin.soc._
val cache = new Soc[String, String](new TimeExpiredRule(60000))
// returns "hello world"
val cached1 = cache.get("cache-key") { "hello world" }
// still returns "hello world"
val cached2 = cache.get("cache-key") { "never gets called" }
// based on the rules provided in the Soc constructor, this dictates
// that cache elements expire after 60000 msecs.
Thread.sleep(60000)
// will re-execute the function to re-populate the cache, will
// return "hello again world"
val cache3 = cache.get("cache-key") { "hello again world" }
<dependencies>
<dependency>
<groupId>com.shorrockin</groupId>
<artifactId>soc</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>shorrockin.com</id>
<name>Shorrockin Repository</name>
<url>http://maven.shorrockin.com/</url>
</repository>
</repositories>