我正在尝试使用Caffeine作为LRU缓存,因此首先添加的条目将首先被逐出.跑这段代码:
final Cache<Object, Object> map = Caffeine.newBuilder()
.maximumSize(10)
.initialCapacity(10)
.build();
for (long i=0; i<20;i++) {
map.put(i, i);
}
map.cleanUp();
System.out.println(map.ge.getAllPresent(map.asMap().keySet()));
Run Code Online (Sandbox Code Playgroud)
哪个印刷品:
{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 19=19}
Run Code Online (Sandbox Code Playgroud)
但我期待
{10=10, 11=11, 12=12, 13=13, 14=14, 15=15, 16=16, 17=17, 18=18, 19=19}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Memcached说它使用一个LRU队列进行驱逐(有一些基于平板大小混合的规则.)当他们说最近最少使用时,他们是指最近最少存储或最近读取的?他们的文件似乎含糊不清.
我正在用Swift构建一个应用程序,我想在我的应用程序中使用LRU Cache.我LRUCache<K: Hashable, V>在Swift中实现了一个简单但后来我认为因为它已经附带了Dictionary和Array集合,所以我可能会错过一个更好的原生选项.
我检查了文档和其他问题,但找不到任何相关内容.
所以我的问题是:Swift是否带有LRUCache?如果是这样,我该如何使用它,如果没有:我可以使用ObjectiveC版本并仍然保持我的Swift类型安全吗?
我想删除的最年长的成员LinkedHashSet,我知道有一个removeEldestEntry,我要重写(方法为removeEldestEntry的Java文档
),但我想我必须定义initial capacity和load factor我不在乎,我只是想删除最近访问过的元素(这里通过访问我的意思是put当它已经在集合中或被读取时)
有没有办法不覆盖removeEldestEntry?
更新:嘿家伙感谢回复.昨晚和今晚我尝试了几种不同的方法,并提出了类似于Jeff下面列出的方法(我甚至已经完成了他在更新中建议的内容,并将我自己的简单LL实现放在一起以获得额外收益).这是代码,此时它看起来并不特别干净,但是我已经多次改变了我可以做的任何事情以增强性能.
public class NewLRU2<K, V> where V : class
{
int m_iMaxItems;
Dictionary<K, LRUNode<K, V>> m_oMainDict;
private LRUNode<K,V> m_oHead;
private LRUNode<K,V> m_oTail;
private LRUNode<K,V> m_oCurrent;
public NewLRU2(int iSize)
{
m_iMaxItems = iSize;
m_oMainDict = new Dictionary<K, LRUNode<K,V>>();
m_oHead = null;
m_oTail = null;
}
public V this[K key]
{
get
{
m_oCurrent = m_oMainDict[key];
if (m_oCurrent == m_oHead)
{
//do nothing
}
else if (m_oCurrent == m_oTail)
{
m_oTail = m_oCurrent.Next;
m_oTail.Prev = null;
m_oHead.Next = m_oCurrent;
m_oCurrent.Prev = m_oHead; …Run Code Online (Sandbox Code Playgroud) 我有6,00,000 entries in MongoDB以下格式:
feature:category:count
Run Code Online (Sandbox Code Playgroud)
哪里
我想缓存前1000个元组,让我们说不要每次都查询数据库.
如何在Python中构建LRU缓存?或者有任何已知的解决方案吗?
我正在使用Python 3的内置functools.lru_cache装饰器来记忆一些昂贵的功能.我想在不使用太多内存的情况下记住尽可能多的调用,因为缓存太多的值会导致颠簸.
是否有一种首选的技术或库可以在Python中实现这一目的?
例如,这个问题引导我到Go库进行系统内存感知LRU缓存.类似Python的东西是理想的.
注意:我不能只估计每个值使用的内存并相应地设置maxsize,因为几个进程将并行调用修饰函数; 一个解决方案需要实际动态检查有多少内存是免费的.
这可能就像在棍子上求月亮一样;但是是否有“C# 生产质量线程安全的内存中 LRU 缓存与到期?或者有没有人有最佳实践的想法来实现同样的事情?
(LRU 是“最近最少使用” - http://en.wikipedia.org/wiki/Cache_algorithms#LRU)
澄清一下:我想在具有以下接口的 ASP.Net MVC 站点中支持内存缓存:
public interface ICache
{
T GetOrAdd<T>(string key, Func<T> create, TimeSpan timeToLive) where T : class;
bool Remove(string key);
}
Run Code Online (Sandbox Code Playgroud)
Microsoft 的最佳解决方案似乎是“System.Runtime.Caching.MemoryCache”,但它似乎有几个警告:
代码如下所示:
public sealed class Cache : ICache
{
private readonly MemoryCache _cache;
public Cache()
{
_cache = MemoryCache.Default;
}
public T GetOrAdd<T>(string key, Func<T> create, TimeSpan timeToLive) where T : class
{
// This call kinda defeats …Run Code Online (Sandbox Code Playgroud) 这是一个简化的函数,我试图为其添加一个lru_cachefor -
from functools import lru_cache, wraps
@lru_cache(maxsize=1000)
def validate_token(token):
if token % 3:
return None
return True
for x in range(1000):
validate_token(x)
print(validate_token.cache_info())
Run Code Online (Sandbox Code Playgroud)
输出 -
CacheInfo(hits=0, misses=1000, maxsize=1000, currsize=1000)
Run Code Online (Sandbox Code Playgroud)
正如我们所看到的,它也会缓存args返回returned值None。在上面的示例中,我希望cache_size为 334,我们返回非 None 值。就我而言,我的函数有很大的编号。如果之前的值为 ,则ofargs可能会返回不同的值None。所以我想避免缓存None值。
我想避免重新发明轮子并lru_cache从头开始实施。有什么好的方法可以做到这一点吗?
以下是我的一些尝试 -
1.尝试实现自己的缓存(这里不是lru)-
from functools import wraps
# global cache object
MY_CACHE = {}
def get_func_hash(func):
# generates unique key for a function. TODO: …Run Code Online (Sandbox Code Playgroud) 我需要编写一个 sql 数据库列 getter,它接受列名和时间,并返回与输入时间相对应的该列的整列值。这可能是具有相同参数的频繁函数调用,因此我想使用 lru 缓存。但是,我不确定列名的频率是否均匀分布,因此理想情况下,我将为每个列名都有一个单独的 lru 缓存。
I previously had it like below, but I would like to separate the lru for each col_name.
@lru_cache(...)
def get_col(self, col_name, time)
# do stuff to get the column and return it
Run Code Online (Sandbox Code Playgroud)
How can I achieve this? Also, unfortunately, I have to support py2.
lru ×10
python ×4
caching ×2
java ×2
.net ×1
asp.net-mvc ×1
c# ×1
caffeine ×1
collections ×1
hashmap ×1
ios ×1
memcached ×1
mongodb ×1
performance ×1
python-2.x ×1
python-3.x ×1
swift ×1