标签: lru

算法LRU,实现这个算法需要多少比特?

我对算法LRU有一点疑问.如果您有一个包含四个块的缓存,那么您需要多少位才能实现此算法?

algorithm lru cpu-cache

6
推荐指数
2
解决办法
1万
查看次数

如何在CPU中实现LRU缓存?

我正在研究面试,并希望在缓存方面重温我的记忆.如果CPU具有带LRU替换策略的缓存,那么芯片上实际实现的是什么?每个缓存行都会存储时间戳记吗?

在双核系统中会发生什么,两个CPU同时写入一个地址?

cpu caching cpu-architecture lru cpu-cache

6
推荐指数
1
解决办法
4156
查看次数

获取上次访问给定Redis密钥的时间

我想查看我的redis服务器上特定密钥的最近访问时间.

我知道这些信息是为每个密钥存储的,因为它在redis的LRU算法中用于消除旧密钥.

是否有一种简单的方法可以查看给定密钥的此信息?

caching key lru redis

6
推荐指数
2
解决办法
6571
查看次数

Java中的LRU缓存实现

我已经看到了以下代码,我认为在addElement方法的实现中有一个无用的while循环.它应该永远不会出现比size + 1更多的元素,因为已经存在写锁定.那么为什么addElement方法删除元素直到它得到这个条件为真

while(concurrentLinkedQueue.size() >=maxSize)
Run Code Online (Sandbox Code Playgroud)

围绕这个的任何指针都会很棒.

这是实施:

public class  LRUCache<K,V> {

    private  ConcurrentLinkedQueue<K> concurrentLinkedQueue = new ConcurrentLinkedQueue<K>();

    private  ConcurrentHashMap<K,V> concurrentHashMap = new ConcurrentHashMap<K, V>();

    private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    private Lock readLock = readWriteLock.readLock();

    private Lock writeLock = readWriteLock.writeLock();

    int maxSize=0;

    public LRUCache(final int MAX_SIZE){
        this.maxSize=MAX_SIZE;
    }

    public V getElement(K key){

        readLock.lock();
        try {
        V v=null;
          if(concurrentHashMap.contains(key)){
              concurrentLinkedQueue.remove(key);
              v= concurrentHashMap.get(key);
                concurrentLinkedQueue.add(key);
          }


        return v;
        }finally{
            readLock.unlock();
        }
    }

    public V removeElement(K key){
         writeLock.lock();
         try {
        V v=null;
        if(concurrentHashMap.contains(key)){
        v=concurrentHashMap.remove(key); …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading caching lru

6
推荐指数
1
解决办法
6349
查看次数

清除 Python 中的所有 lru_cache

我在 python 中有带有 lru_cache 缓存的函数,例如

 @lru_cache(maxsize=None)
 def my_function():
    ...
Run Code Online (Sandbox Code Playgroud)

虽然我可以单独清除缓存,例如my_function.cache_clear()有没有办法一次清除每个函数的缓存?[我在想,也许有一种方法可以返回内存中加载的所有函数名称,然后循环遍历它们以清除每个函数的缓存]。

我特别希望作为回退的一部分来实现,例如在我的机器上 90% 的内存被使用的情况下。

python caching lru

6
推荐指数
2
解决办法
7267
查看次数

可以从 functools.lru_cache 中替换或删除特定键吗?

我正在使用 afunctools.lru_cache来提供给定输入* 的临时文件路径。但是,如果路径不再存在,我想删除/替换单个相应的键。该cache_clear()方法将是矫枉过正,cache_info()似乎没有帮助。

谢谢你的帮助!

*被缓存的方法将文件对象从 S3 流式传输到本地临时文件。

python caching lru python-3.x functools

6
推荐指数
1
解决办法
2046
查看次数

Python lru_cache:currsize &lt; misses &lt; maxsize 如何?

我有一个类,其中的方法用注释进行了lru_cache注释

CACHE_SIZE=16384

class MyClass:
    [...]

    @lru_cache(maxsize=CACHE_SIZE)
    def _my_method(self, texts: Tuple[str]):
       <some heavy text processing>

    def cache_info(self):
        return self._my_method.cache_info()

Run Code Online (Sandbox Code Playgroud)

运行一段时间后,我通过方法查看缓存统计信息cache_info()

c = MyClass()
[...]
c.cache_info()

{
  "hits":9348,
  "misses":4312,
  "maxsize":16384,
  "currsize":2588
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何currsize小于missesAND 小于maxsize

我的理解:对于每次未命中,结果都会添加到缓存中,从而增加当前大小。仅当当前大小达到最大大小时,才会删除缓存结果。由于此处尚未达到最大大小,因此应缓存每个未命中,因此此时currsize应相等。misses然而,这似乎并不是它的工作方式。

python caching lru

6
推荐指数
1
解决办法
606
查看次数

当参数是对象实例并且缓存需要查看实例属性时,如何 lru_cache 函数?

我正在尝试在一个以 python 对象作为参数的函数上实现 lru_cache 。仅当函数的参数属性未更改时,该函数才应返回缓存值。然而,看起来 lru_cache 只对函数参数进行“浅层查看”以查看发生了什么变化,并忽略任何属性更改。

例如,在下面的函数中calculate,用 修饰的函数lru_cache接受单元格实例并返回基于实例属性的计算。

from functools import lru_cache

class Cell:

    def __init__(self, x, y):
        self.x =x
        self.y =y


@lru_cache()
def calculate(cell):
    return cell.x + cell.y
Run Code Online (Sandbox Code Playgroud)

运行此命令时:

if __name__ == '__main__':

    cellA = Cell(1,2)
    print(calculate(cellA))
    #returns 3 correctly

    #let's change cellA's attribute of x to something else
    cellA.x = 10

    print(calculate(cellA))
    #also returns 3, but should return 12!
Run Code Online (Sandbox Code Playgroud)

我希望对函数的第二次调用实际上使用缓存的值,因为属性 x 现已更改。

一个非常不优雅的解决方法是将“假参数”传递给计算函数,如下所示:

@lru_cache()
def calculate(cell, prec):
    return cell.x + cell.y …
Run Code Online (Sandbox Code Playgroud)

caching lru python-3.x

5
推荐指数
0
解决办法
884
查看次数

Python LRU 缓存的全局和每个实例的行为差异

我正在研究Python 的 LRU 缓存装饰器的实现细节。为了正确理解装饰器在不同场景中的行为lru_cache,我还浏览了以下答案:

\n\n

到目前为止,我可以看出这三种情况下的缓存行为是不同的:

\n
    \n
  1. 装饰全局命名空间中的函数。
  2. \n
  3. 装饰类中的实例方法。
  4. \n
  5. 装饰类中的方法,稍后用staticmethodorclassmethod装饰器装饰。
  6. \n
\n

第一种情况是快乐路径,其中用lru_cache装饰器装饰的每个函数都有自己的缓存。这已经有据可查。在第二种情况下,缓存在类的多个实例之间共享,其中每个实例对于实例方法的相同参数将具有不同的键。这在我列出的最后一个问题中得到了很好的解释。在第三种情况下,缓存也在封装类的多个实例之间共享。但是,由于静态方法或类方法不将self其作为第一个参数,因此类的实例不会为相同的参数创建单独的缓存条目。

\n

我的问题是\xe2\x80\x94什么实现细节定义了这种行为?在函数的实现中lru_cache,我只能看到函数内部的本地缓存字典_lru_cache_wrapper正在保存缓存条目。这是片段:

\n
def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):\n    \n    sentinel = object()          \n    make_key = _make_key         \n    PREV, NEXT, KEY, RESULT = 0, 1, …
Run Code Online (Sandbox Code Playgroud)

python caching lru python-3.x

5
推荐指数
0
解决办法
1042
查看次数

如何缓存剧作家的结果,以便下次调用相同的 URL 时,它可以直接从缓存中提取?

我正在反应应用程序中运行剧作家。我的目标是立即返回缓存结果的结果。

下面是能够设置和获取缓存。但下次请求相同的 URL 时,它的行为就好像它没有保存在缓存中一样。我缺少什么?

const { chromium } = require('playwright');
const LRU = require('lru-cache');

const cache = new LRU({
  max: 500,  // Max number of entries in the cache
  ttl: 86400 // Time to live for an entry in the cache, in milliseconds
});

async function fetchHTML(url) {
  const browser = await chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();
  page.setDefaultTimeout(10000);
  // Check if the URL is in the cache
  let html = cache.get(url);
  if (html) { …
Run Code Online (Sandbox Code Playgroud)

javascript caching lru playwright

5
推荐指数
0
解决办法
1130
查看次数