我想创建一个有效的LRU缓存实现.我发现最方便的方法是使用,LinkedHashMap但不幸的是,如果许多线程使用缓存,它会很慢.我的实现在这里:
/**
* Class provides API for FixedSizeCache.
* Its inheritors represent classes
* with concrete strategies
* for choosing elements to delete
* in case of cache overflow. All inheritors
* must implement {@link #getSize(K, V)}.
*/
public abstract class FixedSizeCache <K, V> implements ICache <K, V> {
/**
* Current cache size.
*/
private int currentSize;
/**
* Maximum allowable cache size.
*/
private int maxSize;
/**
* Number of {@link #get(K)} queries for which appropriate …Run Code Online (Sandbox Code Playgroud) 当出现页面错误或高速缓存未命中时,我们可以使用最近最少使用(LRU),先进先出(FIFO)或随机替换算法.我想知道,哪一个提供最佳性能,又称最不可能的未来缓存未命中'/页面错误?
架构:Coldfire处理器
我经常在Python中进行交互式工作,涉及一些我不想经常重复的昂贵操作.我通常运行我正在处理的任何Python文件.
如果我写:
import functools32
@functools32.lru_cache()
def square(x):
print "Squaring", x
return x*x
Run Code Online (Sandbox Code Playgroud)
我得到这个行为:
>>> square(10)
Squaring 10
100
>>> square(10)
100
>>> runfile(...)
>>> square(10)
Squaring 10
100
Run Code Online (Sandbox Code Playgroud)
也就是说,重新运行文件会清除缓存.这有效:
try:
safe_square
except NameError:
@functools32.lru_cache()
def safe_square(x):
print "Squaring", x
return x*x
Run Code Online (Sandbox Code Playgroud)
但是当函数很长时,将它的定义放在一个try块中感觉很奇怪.我可以这样做:
def _square(x):
print "Squaring", x
return x*x
try:
safe_square_2
except NameError:
safe_square_2 = functools32.lru_cache()(_square)
Run Code Online (Sandbox Code Playgroud)
但感觉非常人为(例如,在没有'@'符号的情况下调用装饰器)
是否有一种简单的方法来处理这个问题,例如:
@non_resetting_lru_cache()
def square(x):
print "Squaring", x
return x*x
Run Code Online (Sandbox Code Playgroud)
?
我想在我的代码中使用lru_cache,但是,我收到此错误:
NameError: name 'lru_cache' is not defined
Run Code Online (Sandbox Code Playgroud)
我的代码中有一个导入functools,但这没有帮助
示例代码在这里:
https://docs.python.org/3/library/functools.html
@lru_cache(maxsize=None)
def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)
Run Code Online (Sandbox Code Playgroud)
我正在研究LRU并使用下面的代码片段来理解它的实现.请告诉我map.remove(key)方法调用的用途get(int key).我们不能只使用map.put(key,value),这将更新地图中的密钥条目.
我在下面代码.
class LRUMap<K, V> extends LinkedHashMap<K, V>{
private final int MAX_NUM;
public LRUMap(int capacity) {
super(capacity);
MAX_NUM = capacity;
}
protected boolean removeEldestEntry(Map.Entry eldest) {
return /*true*/ size() > MAX_NUM;
}
}
public class LRU {
LRUMap<Integer, Integer> map;
public LRU(int capacity) {
map = new LRUMap<Integer, Integer>(capacity);
}
public int get(int key) {
if (map == null || map.get(key) == null)
return -1;
int value = map.get(key);
map.remove(key);
map.put(key, value);
return …Run Code Online (Sandbox Code Playgroud) 在Perl中是否有类似于LinkedHashMap的数据结构?
或Perl中的LRU数据结构
更新:@TLP基本上我想要Hashtable数据结构,但我也可以保持键的顺序进入,在我处理列表中的键后删除键.
更新:@ccheneson Tie :: IxHash似乎不是我想要的,我想POP一个最旧的键,但tie :: ixHash弹出最新的键,我如何获得Tie :: IxHash中最旧的键值对?我想要一个队列结构(和哈希结构,我想以最简单的方式找到密钥),新密钥,值对继续进入,我保持进程最旧的密钥并删除最旧的密钥.
更新:@ FMc Tie :: IxHash是我需要的,Tie :: IxHash-> Shift()执行队列弹出Tie :: IxHash-> Push()执行队列推送,它是哈希结构,易于查找键.
谢谢大家.
我想从最近最少使用列表 (LRU) 中隐藏我的应用程序 - 当您长按主页按钮时显示的列表。可以做到吗?谢谢。
编辑:这个应用程序做到了:https : //www.youtube.com/watch?v=LBNwCZX8Cbo#t=99
所以似乎可以做到。
我有一个规范试图在其中定义LRU Cache系统,而我遇到的问题之一就是如何从结构键/值对(基本上是字典或哈希表)中删除值其他语言)。
这是到目前为止的规范本身(不完整):
EXTENDS Integers, Sequences
VARIABLES capacity, currentSize, queue, dictionary
Init == /\ (capacity = 3 ) /\ (currentSize = 0)
/\ (queue = <<>>) /\ (dictionary = [])
AddItem(Item, Value) == IF currentSize < capacity
THEN /\ currentSize' = currentSize + 1
/\ queue' = Append(queue, Item)
/\ dictionary[item] = value
ELSE /\ queue' = Append(Tail(queue), Item)
/\ dictionary' = [x \in dictionary: x /= queue[3]]
GetItem(Item) == dictionary[item]
Next == \/ AddItem
\/ GetItem
Run Code Online (Sandbox Code Playgroud)