Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #611 from matrix-org/erikj/expiring_cache_size
Browse files Browse the repository at this point in the history
Report size of ExpiringCache
  • Loading branch information
erikjohnston committed Mar 1, 2016
2 parents bfdcc7b + 72165e5 commit 742ec37
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions synapse/util/caches/expiringcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from synapse.util.caches import cache_counter, caches_by_name

import logging


Expand Down Expand Up @@ -47,6 +49,8 @@ def __init__(self, cache_name, clock, max_len=0, expiry_ms=0,

self._cache = {}

caches_by_name[cache_name] = self._cache

def start(self):
if not self._expiry_ms:
# Don't bother starting the loop if things never expire
Expand All @@ -72,7 +76,12 @@ def __setitem__(self, key, value):
self._cache.pop(k)

def __getitem__(self, key):
entry = self._cache[key]
try:
entry = self._cache[key]
cache_counter.inc_hits(self._cache_name)
except KeyError:
cache_counter.inc_misses(self._cache_name)
raise

if self._reset_expiry_on_get:
entry.time = self._clock.time_msec()
Expand Down Expand Up @@ -105,9 +114,12 @@ def _prune_cache(self):

logger.debug(
"[%s] _prune_cache before: %d, after len: %d",
self._cache_name, begin_length, len(self._cache.keys())
self._cache_name, begin_length, len(self._cache)
)

def __len__(self):
return len(self._cache)


class _CacheEntry(object):
def __init__(self, time, value):
Expand Down

0 comments on commit 742ec37

Please sign in to comment.