-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from lotress/master
4.5.4
- Loading branch information
Showing
42 changed files
with
1,587 additions
and
1,144 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from collections import deque | ||
|
||
Null = lambda *_: None | ||
|
||
class Cache(): | ||
def __init__(self, size, default=None, onExtinct=Null): | ||
self.cache = {} | ||
self.size = size | ||
self.queue = deque() | ||
self.default = default | ||
self.extinct = onExtinct | ||
|
||
def put(self, key, item): | ||
if len(self.queue) == self.size: | ||
while len(self.queue): | ||
oldKey = self.queue.popleft() | ||
if oldKey in self.cache: | ||
oldItem = self.cache[oldKey] | ||
del self.cache[oldKey] | ||
self.extinct(oldKey, oldItem) | ||
break | ||
self.cache[key] = item | ||
self.queue.append(key) | ||
|
||
def pop(self, key): | ||
if key in self.cache: | ||
res = self.cache[key] | ||
del self.cache[key] | ||
return res | ||
else: | ||
return self.default | ||
|
||
def peek(self, key): | ||
return key in self.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.