本文整理汇总了Python中repoze.lru.LRUCache.get方法的典型用法代码示例。如果您正苦于以下问题:Python LRUCache.get方法的具体用法?Python LRUCache.get怎么用?Python LRUCache.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类repoze.lru.LRUCache
的用法示例。
在下文中一共展示了LRUCache.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LRUCache
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
class LRUCache(StorageInterface):
"""In memory LRU cache"""
def __init__(self, max_size=1024):
if max_size < 1:
raise ValueError("max_size must be a positive integer greater than 0")
self.max_size = max_size
self.engine = LRUCacheEngine(max_size)
def __getitem__(self, key):
value = self.engine.get(key)
if value is None:
raise KeyError
return value
def __setitem__(self, key, value):
self.engine.put(key, value)
def __contains__(self, key):
value = self.engine.get(key)
if value is not None:
return True
else:
return False
def __len__(self):
return self.max_size
示例2: AdapterRegistry
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
class AdapterRegistry(object):
""" Registry of adapters"""
_sentinel = object()
def __init__(self):
self.underlying = adapter.AdapterRegistry()
self.cache = LRUCache(500)
def lookup_adapter(self, typ):
""" Lookup adapter for ``typ``"""
adapter = self.cache.get(typ, self._sentinel)
if adapter is self._sentinel:
adapter = self.underlying.lookup([typ], IJSONSerializeable, "")
self.cache.put(typ, adapter)
return adapter
def register_adapter(self, typ, adapter=None):
""" Register ``adapter`` for type ``typ``
If no ``adapter`` supplied then this method returns decorator.
"""
if adapter is None:
def decorator(adapter):
self.register_adapter_impl(typ, adapter)
return adapter
return decorator
return self.register_adapter_impl(typ, adapter)
def register_adapter_impl(self, typ, adapter):
self.underlying.register(
[implementedBy(typ)], IJSONSerializeable, "", adapter)
self.cache.clear()
示例3: StaticsMiddleware
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
class StaticsMiddleware(object):
def _adapt_path(self, path):
return normcase(normpath(path))
def __init__(self, app, root_dir, cache_max_age=3600):
self.app = app
self.cache_max_age = cache_max_age
self.doc_root = self._adapt_path(root_dir)
self.paths_cache = LRUCache(1024)
def __call__(self, environ, start_response):
full_path = environ['PATH_INFO']
filepath = self.paths_cache.get(full_path)
if filepath is None:
path = full_path.split('/')
if INVALID_PATH_PARTS(path):
return HTTPNotFound('Out of bounds: %s' % environ['PATH_INFO'])(environ, start_response)
filepath = self._adapt_path(join(self.doc_root, *path))
self.paths_cache.put(full_path, filepath)
if isfile(filepath):
return FileServeApp(filepath, self.cache_max_age)(environ, start_response)
return self.app(environ, start_response)
示例4: RMemorySessionStore
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
class RMemorySessionStore(Singleton):
def __init__(self, config):
self._cache = LRUCache(config.session_cache_size)
def push(self, token_id, data):
self._cache.put(token_id, data)
def get(self, token):
return self._cache.get(token, None)
def remove(self, token_id):
try:
self._cache.put(token_id, None)
except KeyError:
pass
def contains(self, session_id):
return self._cache.get(session_id) is not None
示例5: EDBag
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
class EDBag(Counter):
def __init__(self):
super(EDBag, self).__init__()
self.cache1 = LRUCache(256) # values where distance=1
self.cache2 = LRUCache(256) # values where distance>1
def add(self, x):
if not x in self:
self.cache2.clear()
self[x] += 1
def closest_by_edit_distance(self, x):
if x in self:
# Optimization: if x is in multiset, then closest
# edit dist = 0. Nothing can be any closer.
return (x, 0)
# Optimization: If we've looked up this value before,
# return previously computed answer.
cached_answer = self.cache1.get(x)
if cached_answer:
return cached_answer
cached_answer = self.cache2.get(x)
if cached_answer:
return cached_answer
closest = None
closest_dist = None
for y,_ in self.most_common():
d = editdistance.eval(x, y)
if not closest_dist or d < closest_dist:
closest = y
closest_dist = d
if d == 1:
# Optimization: nothing can be any closer, as
# we know there's nothing at edit distance 0 (x is not
# in the multiset).
self.cache1.put(x, (closest, closest_dist))
return (closest, closest_dist)
self.cache2.put(x, (closest, closest_dist))
return (closest, closest_dist)
示例6: RMemorySessionStore
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
class RMemorySessionStore(RUtils.singleton.Singleton):
def __init__(self):
if hasattr(self, '_init'):
return
self._init = True
self.config = RUtils.config.RConfig()
self._cache = LRUCache(self.config.session_cache_size)
def push(self, token_id, data):
self._cache.put(token_id, data)
def get(self, token):
return self._cache.get(token, None)
def remove(self, session_id):
try:
self._cache.put(session_id, None)
except KeyError:
pass
def contains(self, session_id):
return self._cache.get(session_id) is not None
示例7: ClusterCache
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
class ClusterCache(object):
def __init__(self, cache_size):
self.lru = LRUCache(cache_size)
self.hits = 0
self.misses = 0
def get(self, file_buffer, ptr):
v = self.lru.get((file_buffer, ptr))
if v is not None:
self.hits += 1
return v
v = ClusterData(file_buffer, ptr)
self.lru.put((file_buffer, ptr), v)
self.misses += 1
return v
def clear(self):
logger.debug("CACHE HITS " + str(self.hits) + " VS MISSES " + str(self.misses))
self.lru.clear()
示例8: LRUShelf
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
class LRUShelf(Shelf):
"""An in-memory Least-Recently Used shelf up to `maxsize`.."""
def __init__(self, maxsize=1000):
self.store = LRUCache(int(maxsize))
def getitem(self, key):
value = self.store.get(key, UNSET)
if value is UNSET:
raise KeyError(key)
return value
def setitem(self, key, value):
self.store.put(key, value)
def delitem(self, key):
self.store.invalidate(key)
def clear(self):
self.store.clear()
示例9: partly_distinct
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
def partly_distinct(iterable):
"""
Filters items from iterable and **tries to return only distincts**.
Keeps order.
:param Iterable iterable: Something iterable we have to filter.
>>> list(partly_distinct([1, 2, 3, 2, 1, 2, 3, 4]))
... [1, 2, 3, 4]
.. note::
Unlike :py:func:`distinct` it won't guarantee that all elements would
be distinct. But if you have rather small cardinality of the stream,
this would work.
.. note::
Current implementation guarantees support for 10000 distinct values.
If your cardinality is bigger, there might be some duplicates.
"""
cache = LRUCache(10000)
for item in iterable:
if not cache.get(item):
cache.put(item, True)
yield item
示例10: Registry
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
class Registry(object):
""" A component registry. The component registry supports the
Python mapping interface and can be used as you might a regular
dictionary. It also support more advanced registrations and
lookups that include a ``requires`` argument and a ``name`` via
its ``register`` and ``lookup`` methods. It may be treated as an
component registry by using its ``resolve`` method."""
def __init__(self, dict=None, **kwargs):
self.data = {}
self._lkpcache = LRUCache(1000)
if dict is not None:
self.update(dict)
if len(kwargs):
self.update(kwargs)
self.listener_registered = False # at least one listener registered
@property
def _dictmembers(self):
D = {}
norequires = self.data.get((), {})
for k, v in norequires.items():
provides, name = k
if name == '':
D[provides] = v
return D
def __cmp__(self, dict):
if isinstance(dict, Registry):
return cmp(self.data, dict.data)
else:
return cmp(self._dictmembers, dict)
def __len__(self):
return len(self._dictmembers)
def __getitem__(self, key):
notrequires = self.data.get((), {})
return notrequires[(key, '')]
def __setitem__(self, key, val):
self.register(key, val)
def __delitem__(self, key):
self._lkpcache.clear()
notrequires = self.data.get((), {})
try:
del notrequires[(key, '')]
except KeyError:
raise KeyError(key)
def clear(self, full=False):
if full:
self.data = {}
else:
notrequires = self.data.get((), {})
for k, v in notrequires.items():
provides, name = k
if name == '':
del notrequires[k]
self._lkpcache.clear()
def copy(self):
import copy
return copy.copy(self)
def items(self):
return self._dictmembers.items()
def keys(self):
return self._dictmembers.keys()
def values(self):
return self._dictmembers.values()
def iteritems(self):
return iter(self.items())
def iterkeys(self):
return iter(self.keys())
def itervalues(self):
return iter(self.values())
def __contains__(self, key):
return key in self._dictmembers
has_key = __contains__
def get(self, key, default=None):
try:
return self[key]
except KeyError:
return default
@classmethod
def fromkeys(cls, iterable, value=None):
d = cls()
for key in iterable:
d[key] = value
return d
#.........这里部分代码省略.........
示例11: __init__
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
class Filesystem:
def __init__(self, basedir, notify_on_commit):
self.basedir = basedir
self._notify_on_commit = notify_on_commit
self._changelog_cache = LRUCache(1000) # is thread safe
with self.get_sqlconn() as conn:
row = conn.execute("select max(serial) from changelog").fetchone()
serial = row[0]
if serial is None:
self.next_serial = 0
else:
self.next_serial = serial + 1
# perform some crash recovery
data = self.get_raw_changelog_entry(serial)
changes, rel_renames = loads(data)
check_pending_renames(str(self.basedir), rel_renames)
def write_transaction(self, sqlconn):
return FSWriter(self, sqlconn)
def get_raw_changelog_entry(self, serial):
q = "SELECT data FROM changelog WHERE serial = ?"
with self.get_sqlconn() as conn:
conn.text_factory = bytes
row = conn.execute(q, (serial,)).fetchone()
if row is not None:
return bytes(row[0])
return None
def get_changes(self, serial):
changes = self._changelog_cache.get(serial)
if changes is None:
data = self.get_raw_changelog_entry(serial)
changes, rel_renames = loads(data)
self._changelog_cache.put(serial, changes)
return changes
def cache_commit_changes(self, serial, changes):
self._changelog_cache.put(serial, changes)
def get_sqlconn(self):
path = self.basedir.join(".sqlite")
if not path.exists():
with sqlite3.connect(str(path)) as conn:
threadlog.info("DB: Creating schema")
c = conn.cursor()
c.execute("""
CREATE TABLE kv (
key TEXT NOT NULL PRIMARY KEY,
keyname TEXT,
serial INTEGER
)
""")
c.execute("""
CREATE TABLE changelog (
serial INTEGER PRIMARY KEY,
data BLOB NOT NULL
)
""")
conn = sqlite3.connect(str(path), timeout=60)
return conn
def db_read_typedkey(self, relpath, conn=None):
new_conn = conn is None
if new_conn:
conn = self.get_sqlconn()
q = "SELECT keyname, serial FROM kv WHERE key = ?"
try:
c = conn.cursor()
row = c.execute(q, (relpath,)).fetchone()
if row is None:
raise KeyError(relpath)
return tuple(row[:2])
finally:
if new_conn:
conn.close()
示例12: Context
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
class Context():
def __init__(self):
self.cli = False
self.args = {}
self.debug = False
self.debug2 = False
self.quiet = False
self.profile = False
self.components = OrderedDict()
self.start_item = OrderedDict()
self.start_nodes = []
self.config_files = []
self.included_files = []
self.props = {}
self.properties = self.props
self.var = {}
self.working_dir = os.getcwd()
self.library_path = os.path.dirname(os.path.realpath(__file__)) + "/../../library"
self.comp = Components(self)
self._functions = {"text": functions,
"xml": xmlfunctions,
"datetime": datetime,
"dt": datetime,
"re": re,
"sys": sys,
"urllib": urllib,
"random": random.Random()}
self._globals = self._functions
class Functions():
pass
self.f = Functions()
for k, v in self._functions.items():
setattr(self.f, k, v)
self._compiled = LRUCache(512) # TODO: Configurable
@staticmethod
def _class_from_frame(fr):
try:
class_type = fr.f_locals['self'].__class__
except KeyError:
class_type = None
return class_type
def get(self, uid, fail=True):
#logger.debug("Getting component: %s" % component_id)
if uid is None:
raise ETLException("Cannot retrieve component with id None.")
comp = self.components.get(uid, None)
if comp is None and fail:
raise ETLException("Component not found with id '%s'" % uid)
return comp
def key(self, comp):
for k, c in self.components.items():
if c == comp:
return k
return None
def find(self, type):
result = []
for comp in self.components.values():
if isinstance(comp, type):
result.append(comp)
return result
def add(self, urn, component, description=None):
# FIXME: TODO: Allow anonymous components? these would be exported in-line with their parents.
# This assumes that components are initialized completely (possibly better for config comprehension)
# Also would serve as a hint for deep-swallow copying (anonymous components are always deep copied?)
if urn is None:
raise Exception('Tried to add an object with no URN')
if component is None:
raise Exception('Tried to add a null object')
if not isinstance(component, Component):
raise Exception('Tried to add a non Component object: %s' % component)
if self.components.get(urn, None) is not None:
raise Exception("Tried to add an already existing URN: %s" % urn)
#.........这里部分代码省略.........
示例13: __init__
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
class Context:
def __init__(self):
self.args = {}
self.debug = False
self.debug2 = False
self.quiet = False
self.config_files = []
self.start_node = None
self.start_message = {}
self.props = {}
self._globals = {"text": text, "cubetl": cubetl}
self._compiled = LRUCache(512) # TODO: Configurable
self.comp = Components(self)
@staticmethod
def _class_from_frame(fr):
try:
class_type = fr.f_locals["self"].__class__
except KeyError:
class_type = None
return class_type
def interpolate(self, m, value, data={}):
# TODO: Naive interpolation
# TODO: We are enforcing unicode working around Python Spring seems to give strings, not unicode
# This shall not be necessary and it's possibly bad practice
pos = -1
result = unicode(value)
for dstart, dend in (("${|", "|}"), ("${", "}")):
if pos >= -1:
pos = result.find(dstart)
while pos >= 0:
pos_end = result.find(dend)
expr = result[pos + len(dstart) : pos_end].strip()
compiled = self._compiled.get(expr)
try:
if not compiled:
compiled = compile(expr, "", "eval")
self._compiled.put(expr, compiled)
c_locals = {"m": m, "ctx": self, "cubetl": cubetl}
c_locals.update(data)
res = eval(compiled, self._globals, c_locals)
if self.debug2:
if isinstance(res, basestring):
logger.debug("Evaluated: %s = %r" % (expr, res if (len(res) < 100) else res[:100] + ".."))
else:
logger.debug("Evaluated: %s = %r" % (expr, res))
except (Exception) as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
caller_component = None
frame = inspect.currentframe()
for caller in inspect.getouterframes(frame):
fc = Context._class_from_frame(caller[0])
if isclass(fc) and issubclass(fc, Component):
caller_component = caller[0].f_locals["self"]
break
# logger.error("Error evaluating expression %s on data: %s" % (expr, m))
raise Exception(
'Error evaluating expression "%s" called from %s:\n%s'
% (expr, caller_component, ("".join(traceback.format_exception_only(exc_type, exc_value))))
)
if (pos > 0) or (pos_end < len(result) - (len(dend))):
result = result[0:pos] + unicode(res) + result[pos_end + (len(dend)) :]
pos = result.find(dstart)
else:
# Keep non-string types
result = res
pos = -2
return result
def copy_message(self, m):
return copy.copy(m)
示例14: Service
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
class Service(ServiceBase):
CMD = 'twitch'
SUB_FILE = 'twitch_sublist.p'
CHECK_PERIOD = 300
def __init__(self, name_cache_size=512):
ServiceBase.__init__(self)
self._sublist_lock = Lock()
self._twitch = TwitchEngine()
self._channel_name_cache = LRUCache(name_cache_size)
def _setup_argument(self, cmd_group):
cmd_group.add_argument('-subscribe', nargs='+', func=self._subscribe,
help='Subscribe channels and receive notification when channel goes live.\n'
'ex: {} -subscribe kaydada'.format(self.CMD))
cmd_group.add_argument('-unsubscribe', nargs='+', func=self._unsubscribe,
help='Unsubscribe channels.\n'
'ex: {} -unsubscribe kaydada'.format(self.CMD))
cmd_group.add_argument('-unsuball', action='store_true', func=self._unsub_all,
help="Unsubscribe all channels in Linot. I won't send any notification to you anymore.")
cmd_group.add_argument('-listchannel', action='store_true', func=self._list_channel,
help="List channels you've subscribed.")
cmd_group.add_argument('-import', nargs=1, func=self._import,
help='Import the following list of a twitch user.\n'
'ex: {} -import kaydada'.format(self.CMD))
# below, admin only
cmd_group.add_argument('-refresh', action='store_true', func=self._refresh,
help=argparse.SUPPRESS)
cmd_group.add_argument('-listusers', nargs='*', func=self._list_users,
help=argparse.SUPPRESS)
cmd_group.add_direct_command(self._sub_by_url, 'twitch\.tv/(\w+)[\s\t,]*', re.IGNORECASE)
def _start(self):
# Load subscribe list
try:
logger.debug('Loading subscribe list from file')
self._sublist = pickle.load(open(self.SUB_FILE, 'rb'))
self._calculate_channel_sub_count()
except IOError:
logger.debug('Subscribe list file not found, create empty.')
self._sublist = defaultdict(list)
self._channel_sub_count = defaultdict(int)
self._check_thread = Checker(
self.CHECK_PERIOD, self._twitch, self.get_sublist)
self._check_thread.start()
def _stop(self):
self._check_thread.stop()
def get_sublist(self):
self._sublist_lock.acquire(True)
local_sublist = copy.copy(self._sublist)
self._sublist_lock.release()
return local_sublist
def _sub_by_url(self, match_iter, cmd, sender):
logger.debug('sub by url: ' + str(match_iter))
logger.debug('sub by url, direct cmd: ' + cmd)
self._subscribe(match_iter, sender)
def _calculate_channel_sub_count(self):
self._channel_sub_count = defaultdict(int)
for subr in self._sublist:
for ch in self._sublist[subr]:
self._channel_sub_count[ch] += 1
def _import(self, twitch_user, sender):
# get the following list of twitch_user and subscribe them for sender
user = twitch_user[0]
followed_channels = self._twitch.get_followed_channels(user)
if followed_channels is None:
sender.send_message('Twitch user: {} not found'.format(user))
else:
if len(followed_channels) > 8:
sender.send_message('Number of followed channels is more than 8. It may take a while to process.')
self._subscribe(followed_channels, sender)
def _unsub_all(self, value, sender):
# unsubscribe all channels for sender
# we can not send self._sublist directly, since unsub operates
# self._sublist
user_sub = copy.copy(self._sublist[sender])
self._unsubscribe(user_sub, sender)
def _subscribe(self, chs, sender):
# Handles user request for subscribing channels
# We actually let the LinotServant to follow these channels
# so that we can check if they are online use streams/followed API
# prompt a message to let user know i am still alive...
sender.send_message('Processing ...')
msg = io.BytesIO()
not_found = []
for ch in chs:
check_name = ch.lower()
# reduce api invocation
if check_name in self._sublist[sender]: # pragma: no cover
continue
#.........这里部分代码省略.........
示例15: CachingKeyLookup
# 需要导入模块: from repoze.lru import LRUCache [as 别名]
# 或者: from repoze.lru.LRUCache import get [as 别名]
class CachingKeyLookup(object):
"""
A key lookup that caches.
Implements the read-only API of :class:`Registry`, using
a cache to speed up access.
The cache is LRU.
:param: key_lookup - the :class:`Registry` to cache.
:param component_cache_size: how many cache entries to store for
the :meth:`component` method. This is also used by dispatch
calls.
:param all_cache_size: how many cache entries to store for the
the :meth:`all` method.
:param fallback_cache_size: how many cache entries to store for
the :meth:`fallback` method.
"""
def __init__(self, key_lookup, component_cache_size, all_cache_size,
fallback_cache_size):
self.key_lookup = key_lookup
self.predicate_key = key_lookup.predicate_key
self.key_dict_to_predicate_key = key_lookup.key_dict_to_predicate_key
self.component_cache = LRUCache(component_cache_size)
self.all_cache = LRUCache(all_cache_size)
self.fallback_cache = LRUCache(fallback_cache_size)
def component(self, key, predicate_key):
"""Lookup value in registry based on predicate_key.
If value for predicate_key cannot be found, looks up first
permutation of predicate_key for which there is a value. Permutations
are made according to the predicates registered for the key.
:param key: an immutable for which to look up the predicate_key.
:param predicate_key: an immutable predicate key, constructed
for predicates given for this key.
:returns: a registered value, or ``None``.
"""
result = self.component_cache.get((key, predicate_key), NOT_FOUND)
if result is not NOT_FOUND:
return result
result = self.key_lookup.component(key, predicate_key)
self.component_cache.put((key, predicate_key), result)
return result
def fallback(self, key, predicate_key):
"""Lookup fallback based on predicate_key.
This finds the fallback for the most specific predicate
that fails to match.
:param key: an immutable for which to look up the predicate_key.
:param predicate_key: an immutable predicate key, constructed
for predicates given for this key.
:returns: the fallback value for the most specific predicate
the failed to match.
"""
result = self.fallback_cache.get((key, predicate_key), NOT_FOUND)
if result is not NOT_FOUND:
return result
result = self.key_lookup.fallback(key, predicate_key)
self.fallback_cache.put((key, predicate_key), result)
return result
def all(self, key, predicate_key):
"""Lookup iterable of values registered for predicate_key.
Looks up values registered for all permutations of
predicate_key, the most specific first.
:param key: an immutable for which to look up the values.
:param predicate_key: an immutable predicate key, constructed for
the predicates given for this key.
:returns: An iterable of registered values.
"""
result = self.all_cache.get((key, predicate_key), NOT_FOUND)
if result is not NOT_FOUND:
return result
result = list(self.key_lookup.all(key, predicate_key))
self.all_cache.put((key, predicate_key), result)
return result
def lookup(self):
"""A :class:`Lookup` for this registry.
"""
return Lookup(self)