def __init__(self, store, cache_expiration_time=60, cache_size_in_mb=2000, cache_id=None, cache_dir='/tmp/cloudfusion/'): """ :param store: the store whose access should be cached :param cache_expiration_time: the time in seconds until any cache entry is expired :param cache_size_in_mb: Approximate limit of the cache in MB. :param cache_id: Serves as identifier for a persistent cache instance. :param cache_dir: Cache directory on local hard drive disk, default value is */tmp/cloudfusion*. """ #prevent simultaneous access to store (synchronous use of __deepcopy__ by _store SyncThread and a different method): self.store = SynchronizeProxy( store, private_methods_to_synchronize=['__deepcopy__']) if cache_id == None: cache_id = str(random.random()) self.logger = logging.getLogger(self.get_logging_handler()) self.logger.debug("creating CachingStore object") if cache_expiration_time < 240: self.logger.warning( "Be aware of the synchronization issue https://github.com/joe42/CloudFusion/issues/16 \ or to avoid the issue set cache_expiration_time to more than 240 seconds." ) # self.temp_file = tempfile.SpooledTemporaryFile() self.cache_expiration_time = cache_expiration_time self.time_of_last_flush = time.time() self.cache_dir = cache_dir[:-1] if cache_dir[ -1:] == '/' else cache_dir # remove slash at the end cache = PersistentLRUCache( self.cache_dir + "/cachingstore_" + cache_id, cache_expiration_time, cache_size_in_mb) cache.set_resize_intervall(10) self.entries = SynchronizeProxy( cache) #[shares_resource: write self.entries] self.sync_thread = StoreSyncThread(self.entries, self.store, self.logger) self.sync_thread.start()
def test(): threads = [] test_obj = SynchronizeProxy(TestSubject()) for i in range(0, 500): t = TestingThread(test_obj) threads.append(t) for t in threads: t.start() for t in threads: t.join() value_sum = 0 for t in threads: value_sum += t.observed_value print str(value_sum) + "sum" assert value_sum == 0, "Value of test subject has changed to one at least once, which can only be observed with an asynchronous access."