The reflection module is now part of oslo.utils so we should remove our local version and use that version instead; this also goes for the uuidutils module which is now part of oslo.utils as well so we no longer need our local version copied from the incubator... Note that one reflection method `find_subclasses` which was to specific to taskflow is now moved to the misc utility module instead of its prior home in the reflection module. Change-Id: I069881c80b0b2916cc0c414992b80171f7eeb79f
71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (C) 2014 Yahoo! Inc. All Rights Reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
import threading
|
|
|
|
from oslo.utils import reflection
|
|
import six
|
|
|
|
|
|
class ExpiringCache(object):
|
|
"""Represents a thread-safe time-based expiring cache.
|
|
|
|
NOTE(harlowja): the values in this cache must have a expired attribute that
|
|
can be used to determine if the key and associated value has expired or if
|
|
it has not.
|
|
"""
|
|
|
|
def __init__(self):
|
|
self._data = {}
|
|
self._lock = threading.Lock()
|
|
|
|
def __setitem__(self, key, value):
|
|
"""Set a value in the cache."""
|
|
with self._lock:
|
|
self._data[key] = value
|
|
|
|
def __len__(self):
|
|
"""Returns how many items are in this cache."""
|
|
return len(self._data)
|
|
|
|
def get(self, key, default=None):
|
|
"""Retrieve a value from the cache (returns default if not found)."""
|
|
return self._data.get(key, default)
|
|
|
|
def __getitem__(self, key):
|
|
"""Retrieve a value from the cache."""
|
|
return self._data[key]
|
|
|
|
def __delitem__(self, key):
|
|
"""Delete a key & value from the cache."""
|
|
with self._lock:
|
|
del self._data[key]
|
|
|
|
def cleanup(self, on_expired_callback=None):
|
|
"""Delete out-dated keys & values from the cache."""
|
|
with self._lock:
|
|
expired_values = [(k, v) for k, v in six.iteritems(self._data)
|
|
if v.expired]
|
|
for (k, _v) in expired_values:
|
|
del self._data[k]
|
|
if on_expired_callback is not None:
|
|
arg_c = len(reflection.get_callable_args(on_expired_callback))
|
|
for (k, v) in expired_values:
|
|
if arg_c == 2:
|
|
on_expired_callback(k, v)
|
|
else:
|
|
on_expired_callback(v)
|