Here is the problem I've been trying to tackle:Design a thread-safe image caching server that can keep in memory only the ten most recently used images.I chose to implement an LRU cache to solve this as follows:'''This module defines an LRUCache.Constraints:1. May only hold upto ten items at a time.2. Must be able to synchronise multiple requests.3. Must be able to update its cache. '''import randomimport threadingclass LRUCache(object):    def __init__(self, cacheSize=10):        # this variables maps image names to file locations        self.Cache = {}        # this variables maps image names to timestamp of last request        # filed for them. Is volatile - requires local clock to never change,        # or that there is a separate clock service that guarantees global        # consistency.         self.RequestTimestamps = {}        self.CacheSize = cacheSize    def insert(self, imageName, imageLocation):        '''        Insert a new image into our cache. If inserting would exceed our cache size,        then we shall make room for it by removing the least recently used.         '''        with threading.Lock():            if len(self.Cache) == self.CacheSize:                self.removeLeastRecentlyUsed()        self.Cache[imageName] = imageLocation        self.RequestTimestamps[imageName] = time.time()    def get(self, imageName):        '''        Retrieve an image from our cache, keeping note of the time we last         retrieved it.         '''        with threading.Lock():            if imageName in self.Cache:                self.RequestTimestamps[imageName] = time.time()                return self.Cache[imageName]            else:                raise KeyError(Not found!)    def removeLeastRecentlyUsed(self):        '''        Should only be called in a threadsafe context.        '''        if self.RequestTimestamps:            # scan through and find the least recently used key by finding            # the lowest timestamp among all the keys.            leastRecentlyUsedKey = min(self.RequestTimestamps, key=lambda imageName: self.RequestTimestaps[imageName])            # now that the least recently used key has been found,             # remove it from the cache and from our list of request timestamps.            self.Cache.pop(leastRecentlyUsedKey, None)            self.RequestTimestamps.pop(leastRecentlyUsedKey, None)            return        # only called in the event requestTimestamps does not exist - randomly         # pick a key to erase.        randomChoice = random.choice(self.Cache.keys())        self.Cache.pop(randomChoice, None)Questions:Is this actually threadsafe? I'm used to implementing locks on single resources, but not classmethods - would multiple threads, one accessing insert and theother doing a get be forced to synchronised as well? Or is my strategy of creating different locks in each method only able to prevent concurrent individual insert requests? Ignore the GIL.What would be a good strategy to test this code? I have thought of the obvious strategy:a) Insert ten items, make nine requests, attempt to insert a tenth   and check if the item that was removed was the tenth.but I am not sure if this is the only way or if there is a better way to implement it. 
Implementing a thread-safe LRUCache
python;thread safety;cache