@@ -1120,7 +1120,7 @@ def decr(self, key, delta=1, default=0, retry=False):
1120
1120
"""
1121
1121
return self .incr (key , - delta , default , retry )
1122
1122
1123
- def get (
1123
+ def __get (
1124
1124
self ,
1125
1125
key ,
1126
1126
default = None ,
@@ -1154,42 +1154,35 @@ def get(
1154
1154
' AND (expire_time IS NULL OR expire_time > ?)'
1155
1155
)
1156
1156
1157
+ did_cache_hit : bool = False
1158
+
1157
1159
if expire_time and tag :
1158
1160
default = (default , None , None )
1159
1161
elif expire_time or tag :
1160
1162
default = (default , None )
1161
1163
1162
- if not self . statistics and update_column is None :
1164
+ if update_column is None :
1163
1165
# Fast path, no transaction necessary.
1164
1166
1165
1167
rows = self ._sql (select , (db_key , raw , time .time ())).fetchall ()
1166
1168
1167
1169
if not rows :
1168
- return default
1170
+ return ( default , did_cache_hit )
1169
1171
1170
1172
((rowid , db_expire_time , db_tag , mode , filename , db_value ),) = rows
1171
1173
1172
1174
try :
1173
1175
value = self ._disk .fetch (mode , filename , db_value , read )
1174
1176
except IOError :
1175
1177
# Key was deleted before we could retrieve result.
1176
- return default
1178
+ return ( default , did_cache_hit )
1177
1179
1178
1180
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
-
1186
1181
with self ._transact (retry ) as (sql , _ ):
1187
1182
rows = sql (select , (db_key , raw , time .time ())).fetchall ()
1188
1183
1189
1184
if not rows :
1190
- if self .statistics :
1191
- sql (cache_miss )
1192
- return default
1185
+ return (default , did_cache_hit )
1193
1186
1194
1187
(
1195
1188
(rowid , db_expire_time , db_tag , mode , filename , db_value ),
@@ -1199,27 +1192,68 @@ def get(
1199
1192
value = self ._disk .fetch (mode , filename , db_value , read )
1200
1193
except IOError :
1201
1194
# 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 )
1211
1196
1212
1197
if update_column is not None :
1198
+ now = time .time ()
1199
+ update = 'UPDATE Cache SET %s WHERE rowid = ?'
1213
1200
sql (update % update_column .format (now = now ), (rowid ,))
1214
1201
1202
+ did_cache_hit = True
1203
+
1215
1204
if expire_time and tag :
1216
- return (value , db_expire_time , db_tag )
1205
+ return (( value , db_expire_time , db_tag ), did_cache_hit )
1217
1206
elif expire_time :
1218
- return (value , db_expire_time )
1207
+ return (( value , db_expire_time ), did_cache_hit )
1219
1208
elif tag :
1220
- return (value , db_tag )
1209
+ return (( value , db_tag ), did_cache_hit )
1221
1210
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 )
1223
1257
1224
1258
def __getitem__ (self , key ):
1225
1259
"""Return corresponding value for `key` from cache.
0 commit comments