我只是想知道Scala中是否有任何缓存解决方案.我正在寻找类似于Java中的Guava提供的东西.
我应该在Scala中使用Guava吗?Scalaz中是否有包装/皮条客或类似的东西?任何更适合Scala开发者的替代方案?
番石榴提供的是什么:
LoadingCache<Key, Graph> CACHE= CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.removalListener(MY_LISTENER)
.build(
new CacheLoader<Key, Graph>() {
public Graph load(Key key) throws AnyException {
return createExpensiveGraph(key);
}
});
Supplier<Animal> singleAnimalCache = Suppliers.memoizeWithExpiration(animalFromDbSupplier(), 365, TimeUnit.DAYS);
Run Code Online (Sandbox Code Playgroud)
我需要像Guava一样的基本缓存管理.
我们有相同的要求,最终围绕番石榴建造包装纸.我们最近开源了Mango的部分库.如果你不介意额外的依赖,你可以使用它
import org.feijoas.mango.common.cache._
import org.feijoas.mango.common.base.Suppliers._
val MY_LISTENER = (remNot: RemovalNotification[Key, Graph]) => ()
// > MY_LISTENER : RemovalNotification[Key,Graph] => Unit = <function1>
val CACHE = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.removalListener(MY_LISTENER)
.build { (key: Key) => new Graph() }
// > CACHE : LoadingCache[Key,Graph] = <function1>
val animalFromDbSupplier = () => {
// load from db
new Animal()
}
// > animalFromDbSupplier : () => Animal = <function0>
val singleAnimalCache = memoizeWithExpiration(animalFromDbSupplier, 365, TimeUnit.DAYS)
// > singleAnimalCache : () => Animal = Suppliers.memoizeWithExpiration(<function0>, 365, DAYS)
Run Code Online (Sandbox Code Playgroud)
只是添加一个插入我自己的项目的答案,但我推荐ScalaCache.
https://github.com/cb372/scalacache
Scalaz 中是否有包装器/皮条客或类似的东西?
在 Scalaz 7 中,我在学习 Scalaz 第 16 天时Memo
对此进行了一些介绍。
这是 Adam Rosien 在scalaz“为了我们其他人”演讲中谈到的第一件事,所以也请检查一下。他正在使用 Scalaz 6。
归档时间: |
|
查看次数: |
9496 次 |
最近记录: |