2017-06-13 19:22:08 +02:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
"""
|
|
|
|
Caching function for pictures.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2017-10-29 21:04:09 +01:00
|
|
|
|
2017-06-13 19:22:08 +02:00
|
|
|
class MemoryCache(object):
|
2017-10-29 21:04:09 +01:00
|
|
|
"""
|
|
|
|
A cache in memory.
|
|
|
|
"""
|
2017-06-13 19:22:08 +02:00
|
|
|
def __init__(self, on_miss):
|
2017-10-29 21:04:09 +01:00
|
|
|
"""
|
|
|
|
Constructor
|
|
|
|
|
|
|
|
:param on_miss: Function to call to retrieve item when not already
|
|
|
|
cached.
|
|
|
|
"""
|
2017-06-13 19:22:08 +02:00
|
|
|
self.hits = 0
|
|
|
|
self.misses = 0
|
|
|
|
self.map = {}
|
|
|
|
self.on_miss = on_miss
|
|
|
|
|
|
|
|
def get(self, key):
|
2017-10-29 21:04:09 +01:00
|
|
|
"""
|
|
|
|
Get an element from cache. Eventually call ``on_miss`` if the item is
|
|
|
|
not already cached.
|
|
|
|
|
|
|
|
:param key: Key of the element to retrieve.
|
|
|
|
:return: Requested element.
|
|
|
|
"""
|
2017-06-13 19:22:08 +02:00
|
|
|
cached = self.map.get(key, None)
|
|
|
|
if cached is not None:
|
|
|
|
self.hits += 1
|
|
|
|
return cached
|
|
|
|
|
|
|
|
item = self.map[key] = self.on_miss(key)
|
|
|
|
self.misses += 1
|
|
|
|
return item
|
|
|
|
|
|
|
|
def total(self):
|
2017-10-29 21:04:09 +01:00
|
|
|
"""
|
|
|
|
Get the total number of calls (with hits to the cache, or miss and
|
|
|
|
fetching with ``on_miss``) to the cache.
|
|
|
|
|
|
|
|
:return: Total number of item accessing.
|
|
|
|
"""
|
2017-06-13 19:22:08 +02:00
|
|
|
return self.hits + self.misses
|
|
|
|
|
|
|
|
def hit_rate(self):
|
2017-10-29 21:04:09 +01:00
|
|
|
"""
|
|
|
|
Get the hit rate, that is the rate at which we requested an item which
|
|
|
|
was already in the cache.
|
|
|
|
|
|
|
|
:return: The hit rate, in percents.
|
|
|
|
"""
|
2017-06-13 19:22:08 +02:00
|
|
|
assert self.total() > 0
|
|
|
|
return 100 * self.hits // self.total()
|
|
|
|
|
|
|
|
def miss_rate(self):
|
2017-10-29 21:04:09 +01:00
|
|
|
"""
|
|
|
|
Get the miss rate, that is the rate at which we requested an item which
|
|
|
|
was not already in the cache.
|
|
|
|
|
|
|
|
:return: The miss rate, in percents.
|
|
|
|
"""
|
2017-06-13 19:22:08 +02:00
|
|
|
assert self.total() > 0
|
|
|
|
return 100 * self.misses // self.total()
|
|
|
|
|
2017-10-29 21:04:09 +01:00
|
|
|
|
2017-06-13 19:22:08 +02:00
|
|
|
class ImageCache(MemoryCache):
|
2017-10-29 21:04:09 +01:00
|
|
|
"""
|
|
|
|
A cache for images, stored in memory.
|
|
|
|
"""
|
2017-06-13 19:22:08 +02:00
|
|
|
@staticmethod
|
|
|
|
def retrieve_photo(url):
|
2017-10-29 21:04:09 +01:00
|
|
|
"""
|
|
|
|
Helper to actually retrieve photos if not already cached.
|
|
|
|
"""
|
2017-06-13 19:22:08 +02:00
|
|
|
return requests.get(url)
|
|
|
|
|
|
|
|
def __init__(self):
|
2017-10-29 21:04:09 +01:00
|
|
|
super(ImageCache, self).__init__(on_miss=ImageCache.retrieve_photo)
|