Implemented SheerkaOntology
This commit is contained in:
Vendored
+14
-2
@@ -1,12 +1,16 @@
|
||||
from core.global_symbols import NotFound
|
||||
|
||||
|
||||
class FastCache:
|
||||
"""
|
||||
Simplest LRU cache
|
||||
"""
|
||||
|
||||
def __init__(self, max_size=256):
|
||||
def __init__(self, max_size=256, default=None):
|
||||
self.max_size = max_size
|
||||
self.cache = {}
|
||||
self.lru = []
|
||||
self.default = default
|
||||
|
||||
def put(self, key, value):
|
||||
if len(self.cache) == self.max_size:
|
||||
@@ -18,11 +22,19 @@ class FastCache:
|
||||
self.cache[key] = value
|
||||
self.lru.append(key)
|
||||
|
||||
def has(self, key):
|
||||
return key in self.cache
|
||||
|
||||
def get(self, key):
|
||||
try:
|
||||
return self.cache[key]
|
||||
except KeyError:
|
||||
return None
|
||||
if self.default:
|
||||
value = self.default(key)
|
||||
self.put(key, value)
|
||||
return value
|
||||
|
||||
return NotFound
|
||||
|
||||
def evict_by_key(self, predicate):
|
||||
to_remove = []
|
||||
|
||||
Reference in New Issue
Block a user