jasonfan's picture
Add files using upload-large-folder tool
f31fe4e verified
# coding: utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import threading
class RWLock(object):
def __init__(self):
self.__monitor = threading.Lock()
self.__exclude = threading.Lock()
self.readers = 0
def r_lock(self):
with self.__monitor:
self.readers += 1
if self.readers == 1:
self.__exclude.acquire()
def r_un_lock(self):
with self.__monitor:
self.readers -= 1
if self.readers == 0:
self.__exclude.release()
def lock(self):
self.__exclude.acquire()
def un_lock(self):
self.__exclude.release()
class Cache(object):
def __init__(self):
self._cache = {}
self.lock = RWLock()
def put(self, key, value):
try:
self.lock.lock()
self._cache[key] = value
finally:
self.lock.un_lock()
def get(self, key):
try:
self.lock.r_lock()
if key in self._cache:
return self._cache[key]
else:
return None
finally:
self.lock.r_un_lock()
def has_key(self, key):
if key in self._cache:
return True
else:
return False