Skip to content

Commit e2f3f31

Browse files
committed
Non transactional cache hit/miss statistics
1 parent ebfa37c commit e2f3f31

File tree

1 file changed

+61
-27
lines changed

1 file changed

+61
-27
lines changed

diskcache/core.py

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ def decr(self, key, delta=1, default=0, retry=False):
11201120
"""
11211121
return self.incr(key, -delta, default, retry)
11221122

1123-
def get(
1123+
def __get(
11241124
self,
11251125
key,
11261126
default=None,
@@ -1154,42 +1154,35 @@ def get(
11541154
' AND (expire_time IS NULL OR expire_time > ?)'
11551155
)
11561156

1157+
did_cache_hit: bool = False
1158+
11571159
if expire_time and tag:
11581160
default = (default, None, None)
11591161
elif expire_time or tag:
11601162
default = (default, None)
11611163

1162-
if not self.statistics and update_column is None:
1164+
if update_column is None:
11631165
# Fast path, no transaction necessary.
11641166

11651167
rows = self._sql(select, (db_key, raw, time.time())).fetchall()
11661168

11671169
if not rows:
1168-
return default
1170+
return (default, did_cache_hit)
11691171

11701172
((rowid, db_expire_time, db_tag, mode, filename, db_value),) = rows
11711173

11721174
try:
11731175
value = self._disk.fetch(mode, filename, db_value, read)
11741176
except IOError:
11751177
# Key was deleted before we could retrieve result.
1176-
return default
1178+
return (default, did_cache_hit)
11771179

11781180
else: # Slow path, transaction required.
1179-
cache_hit = (
1180-
'UPDATE Settings SET value = value + 1 WHERE key = "hits"'
1181-
)
1182-
cache_miss = (
1183-
'UPDATE Settings SET value = value + 1 WHERE key = "misses"'
1184-
)
1185-
11861181
with self._transact(retry) as (sql, _):
11871182
rows = sql(select, (db_key, raw, time.time())).fetchall()
11881183

11891184
if not rows:
1190-
if self.statistics:
1191-
sql(cache_miss)
1192-
return default
1185+
return (default, did_cache_hit)
11931186

11941187
(
11951188
(rowid, db_expire_time, db_tag, mode, filename, db_value),
@@ -1199,27 +1192,68 @@ def get(
11991192
value = self._disk.fetch(mode, filename, db_value, read)
12001193
except IOError:
12011194
# Key was deleted before we could retrieve result.
1202-
if self.statistics:
1203-
sql(cache_miss)
1204-
return default
1205-
1206-
if self.statistics:
1207-
sql(cache_hit)
1208-
1209-
now = time.time()
1210-
update = 'UPDATE Cache SET %s WHERE rowid = ?'
1195+
return (default, did_cache_hit)
12111196

12121197
if update_column is not None:
1198+
now = time.time()
1199+
update = 'UPDATE Cache SET %s WHERE rowid = ?'
12131200
sql(update % update_column.format(now=now), (rowid,))
12141201

1202+
did_cache_hit = True
1203+
12151204
if expire_time and tag:
1216-
return (value, db_expire_time, db_tag)
1205+
return ((value, db_expire_time, db_tag), did_cache_hit)
12171206
elif expire_time:
1218-
return (value, db_expire_time)
1207+
return ((value, db_expire_time), did_cache_hit)
12191208
elif tag:
1220-
return (value, db_tag)
1209+
return ((value, db_tag), did_cache_hit)
12211210
else:
1222-
return value
1211+
return value, did_cache_hit
1212+
1213+
def get(
1214+
self,
1215+
key,
1216+
default=None,
1217+
read=False,
1218+
expire_time=False,
1219+
tag=False,
1220+
retry=False,
1221+
):
1222+
"""Retrieve value from cache. If `key` is missing, return `default`.
1223+
1224+
Raises :exc:`Timeout` error when database timeout occurs and `retry` is
1225+
`False` (default).
1226+
1227+
:param key: key for item
1228+
:param default: value to return if key is missing (default None)
1229+
:param bool read: if True, return file handle to value
1230+
(default False)
1231+
:param bool expire_time: if True, return expire_time in tuple
1232+
(default False)
1233+
:param bool tag: if True, return tag in tuple (default False)
1234+
:param bool retry: retry if database timeout occurs (default False)
1235+
:return: value for item or default if key not found
1236+
:raises Timeout: if database timeout occurs
1237+
1238+
"""
1239+
data, did_cache_hit = self.__get(
1240+
key, default, read, expire_time, tag, retry
1241+
)
1242+
1243+
if self.statistics:
1244+
self.track_statistics(did_cache_hit)
1245+
return data
1246+
1247+
def track_statistics(self, did_cache_hit: bool):
1248+
"""Track statistics about cache. Can be overridden for custom functionality."""
1249+
cache_hit = 'UPDATE Settings SET value = value + 1 WHERE key = "hits"'
1250+
cache_miss = (
1251+
'UPDATE Settings SET value = value + 1 WHERE key = "misses"'
1252+
)
1253+
if did_cache_hit is True:
1254+
self._sql(cache_hit)
1255+
elif did_cache_hit is False:
1256+
self._sql(cache_miss)
12231257

12241258
def __getitem__(self, key):
12251259
"""Return corresponding value for `key` from cache.

0 commit comments

Comments
 (0)