(Eh)Cache as a Scala Map

Project source is on github @ https://github.com/vinaynair/EhCacheAsScalaMap

Exposing relevant EhCache features as a scala mutable Map provides interesting possibilities for use & integration.
Note by merely switching the ehcache implementation with BigMemory , we can exploit the same set of
features with local offheap in-memory store as well as clustered caches.

Apart from the obvious type-safety with a Map, other obvious features include:-

Add

val m = new CacheAsMap[Int, String]
//add key+value pair using the following syntax
m(1) = "a"
//or
m += (2 -> "b")
//or since its mutable
m+(3->"c")
//or to add 2 cache maps together
val n = new CacheAsMap[Int,String]()
n(4)="d"
n++=m

Remove

m-=2
//or, remove more than one key at a time
n -=(1,2)

Get

println(m(1))

Filter

val m = new CacheAsMap[String, String]()
//add elements
m("apple") = "fruit"
m("potato") = "vegetable"
m("grapes") = "fruit"
//now filter
//For a key value pair, filter results on a value, for example, find all fruits
 val filteredResults=m.filter { case(key,value) => "fruit".equals(value) }

Transform the values using a closure

val m = new CacheAsMap[Int,Int]()
    m(1)=1
    m(2)=2
    m(3)=3
    // for a given key,value pair, increment all the values by 1
    m transform ( (k,v) => v+1 )

Building & running samples

Use SBT and run the unit tests

TODO

  • Figure how to have a BigMemory profile within SBT along with the opensource ehcache to test and try BigMemory features
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s