Last active
August 29, 2015 14:00
-
-
Save sirmes/11297168 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #How to handle caching in a functional way | |
| scala> case class StlCache(pcode : String, stl_pcode : List[String], ttl : Int) | |
| defined class StlCache | |
| scala> val lamer = StlCache("01", List("02", "03"), 100) | |
| lamer: StlCache = StlCache(01,List(02, 03),100) | |
| scala> val lancome = StlCache("04", List("05", "06"), 500) | |
| lancome: StlCache = StlCache(04,List(05, 06),500) | |
| scala> val cache = List(lamer, lancome) | |
| cache: List[StlCache] = List(StlCache(01,List(02, 03),100), StlCache(04,List(05, 06),500)) | |
| //Manipulates values | |
| scala> def bang(s : StlCache) = List(StlCache(s.pcode, s.stl_pcode, s.ttl*3)) | |
| bang: (s: StlCache)List[StlCache] | |
| scala> cache.flatMap(x => bang(x)) | |
| res19: List[StlCache] = List(StlCache(01,List(02, 03),300), StlCache(04,List(05, 06),1500)) | |
| //Expire cache | |
| def expireCache(s : StlCache) = if (s.ttl > 600) List[StlCache]() else List(StlCache(s.pcode, s.stl_pcode, s.ttl)) | |
| def expireCache2(s : StlCache) = if (s.ttl > 300) List[StlCache]() else List(StlCache(s.pcode, s.stl_pcode, s.ttl)) | |
| cache.flatMap(x => expireCache(x)) | |
| //filter | |
| cache.filter(x => x.ttl > 300) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment