淘汰策略很简单,越早插入的数据就越早淘汰。
当然,如果要弄成LRU也是没有问题的,无非在插入新数据之前,判断key是否已经在dict中,如果存在,那么del后再添加接口。
注:代码摘抄scrapy
- from collections import OrderedDict
- class LocalCache(OrderedDict):
-
"""Dictionary with a finite number of keys.
-
-
Older items expires first.
-
-
"""
-
-
def __init__(self, limit=None):
-
super(LocalCache, self).__init__()
-
self.limit = limit
-
-
def __setitem__(self, key, value):
-
while len(self) >= self.limit:
-
self.popitem(last=False)
-
super(LocalCache, self).__setitem__(key, value)
阅读(3158) | 评论(0) | 转发(0) |