我只是想知道 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);
我需要一些基本的缓存管理,就像在 Guava 中一样。
我们有相同的要求,最终围绕 Guava 构建了包装器。我们最近开源了该库的部分内容,称为Mango https://github.com/feijoas/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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)