class MyMemcache(object): def __init__(self, ip='localhost', port=11211): # json_serializer(key, value) def json_serializer(key, value): if type(value) == str: return value, 1 return json.dumps(value), 2 # json_deserializer(key, value, flags) python3 def json_deserializer(key, value, flags): if flags == 1: return value.decode('utf-8') if flags == 2: return json.loads(value.decode('utf-8')) raise Exception("Unknown serialization format") self.ip = ip self.port = port self.client = Client((ip, port), serializer=json_serializer, deserializer=json_deserializer, key_prefix='', encoding='utf8', allow_unicode_keys=True) ''' set_task_result ''' @catch_exception def set_value(self, key, value): # set(key, value, expire=0, noreply=None, flags=None) # result = "-".join(result.split()) expire_second = int(60 * 60 * 24 * 1.2) # expire_second must int self.client.set(key=key, value=value, expire=expire_second) # debug_p('[set_value]', key, value) ''' get_task_result ''' @catch_exception def get_value(self, key, default=''): result = self.client.get(key=key, default=default) debug_p('[get_value]', key, result) return result ''' close ''' def client_close(self): try: self.client.close() except Exception as e: # pass
class BaseModel(object): def __init__(self): self.conn = pymysql.connect(host=CONFIG['DB_HOST'], port=CONFIG['DB_PORT'], user=CONFIG['DB_USER'], password=CONFIG['DB_PASSWORD'], db=CONFIG['DATABASE'], charset='utf8mb4') if CONFIG['MEMCACHED_ADDRESS'] and CONFIG['MEMCACHED_PORT']: host, port = CONFIG['MEMCACHED_ADDRESS'], CONFIG['MEMCACHED_PORT'] self.cache = Client( (host, port), timeout=CONFIG['MEMCACHED_TIMEOUT'], deserializer=lambda k, v, f: str(v, encoding='utf-8') if isinstance(v, bytes) else v) self.cache_expire = CONFIG['MEMCACHED_CACHE_EXPIRE'] def __del__(self): """ close connection :return: None """ if hasattr(self, 'cache'): self.cache.close() if hasattr(self, 'conn'): self.conn.close()
def test_socket_close(self): server = ("example.com", 11211) client = Client(server, socket_module=MockSocketModule()) client._connect() assert client.sock is not None client.close() assert client.sock is None
def test_socket_close(self): server = ("example.com", 11211) client = Client(server, socket_module=MockSocketModule()) client._connect() assert client.sock is not None client.close() assert client.sock is None
def test_socket_close_exception(self): server = ("example.com", 11211) socket_module = MockSocketModule(close_failure=OSError()) client = Client(server, socket_module=socket_module) client._connect() assert client.sock is not None client.close() assert client.sock is None
def test_socket_close_exception(self): server = ("example.com", 11211) socket_module = MockSocketModule(close_failure=OSError()) client = Client(server, socket_module=socket_module) client._connect() assert client.sock is not None client.close() assert client.sock is None
def check_unauth(self, host, port): if self.flag: try: memcache = Client(server=(host, port), connect_timeout=self.timeout, timeout=self.timeout) version = memcache.version() except Exception as e: hook_msg((False, host, port, "Anonymous", "")) else: if version: hook_msg((True, host, port, "Anonymous", "")) self.unauth_result.append(host) finally: memcache.close()
def get_operators_list(): try: global host if host == 'server': client = Client((gMemCachedUrl), serializer=json_serializer, deserializer=json_deserializer) except Exception: return {'status': traceback.format_exc()} try: need_update = False dictCode = 'OPERATORS_LIST' if host == 'server': context = client.get(dictCode) if context is None: need_update = True else: last_updated_datetime = datetime.strptime( context['updated_datetime'], '%Y-%m-%d %H:%M:%S') need_update = needUpdate('', last_updated_datetime, 'day', 0) else: need_update = True if need_update: context = operators() updated_datetime = datetime.today() context['updated_datetime'] = updated_datetime.strftime( '%Y-%m-%d %H:%M:%S') if host == 'server': if need_update: client.set(dictCode, context) client.close() return context except Exception: if host == 'server': client.close() return {'status': traceback.format_exc()}
class MemcacheStore(CacheMixin): def __init__(self, host='127.0.0.1', port=11211, **kwargs): CacheMixin.__init__(self, **kwargs) self._memcache = Client( (host, port), serializer=pack, deserializer=unpack) def __del__(self): try: self._memcache.close() except: pass def set(self, key, value): self._set_cached(key, value) self._memcache.set(key, value) def get(self, key): cached = self._get_cached(key) if cached is not 'None': return cached return self._memcache.get(key, None) __getitem__ = get __setitem__ = set
from pymemcache.client.base import Client if __name__ == '__main__': client = Client(('localhost', 11211)) client.set('titulo_libro', 'Python A Fondo') contador = client.get('contador') if contador is None: client.set('contador', 1) print(client.get('contador')) client.incr('contador', 5) client.incr('contador', 5) print(client.get('contador')) client.close()
def res(): client = Client(('localhost', 11211)) text = client.get('info') client.delete('info') client.close() return text
from pymemcache.client.base import Client import mysql.connector connect_args = { "user": "******", "password": "******", "host": "localhost", "port": 3306, } db = mysql.connector.connect(**connect_args) cursor = db.cursor() memcache = Client(("localhost", 11211)) sql = "SELECT CountryCode, Name FROM world.city WHERE ID = %s" city_id = 130 city = memcache.get(str(city_id)) if city is not None: country_code, name = city.decode("utf-8").split("|") print("memcached: country: {0} - city: {1}".format(country_code, name)) else: cursor.execute(sql, (city_id, )) country_code, name = cursor.fetchone() memcache.set(str(city_id), "|".join([country_code, name]), expire=60) print("MySQL: country: {0} - city: {1}".format(country_code, name)) memcache.close() cursor.close() db.close()
class MemcachedCache_Pool(object): """ an instance that holds a number of connections to a memcached-server with different databases. inserting and retrieval is an operation which is keyed by the database-number as the routing key Parameters ---------- memc_params : dict dictionary with keys [host, port] to the memcached-server instance val_ttl : int the time to life for each value in seconds; value of 0 disables ttl (default=0) wait_on_insert : bool if True, wait on insert operations to successfully complete before continuing (default: False) flush_on_del : bool flush the databases when the object is destroyed (default: False) """ def __init__(self, memc_params=_DEFAULT_MEMCACHE_CRED, val_ttl=0, wait_on_insert=False, flush_on_del=False): self.memc_params = memc_params self.val_ttl = val_ttl self.wait_on_insert = wait_on_insert self.flush_on_del = flush_on_del def setup(self): self.client = Client((self.memc_params['host'], self.memc_params['port']), default_noreply=not self.wait_on_insert) return self def teardown(self): if self.flush_on_del: self.client.flush_all(delay=0, noreply=None) self.client.close() return self def insert(self, routingkey, key, obj): """ insert a _state_ into the database _rk_, at the key _state.key_ Parameters ---------- routingkey : int >= 0 the integer of the database to store this state to key : int or str the key at which this object is to be stored obj : class the object to be stored """ cache_key = str(routingkey) + '_' + key obj_pickle = pickle.dumps(obj) self.client.set(key=cache_key, value=obj_pickle, expire=self.val_ttl) def fetch(self, routingkey, key): """ insert a _state_ into the database _rk_, at the key _state.key_ Parameters ---------- routingkey : int >= 0 the integer of the database to store this state to key : str/int the unique identifier, same as _state.key_, for which the state will be retrieved Returns ------- obj : the object from the store """ cache_key = str(routingkey) + '_' + str(key) obj_pickle = self.client.get(cache_key) if obj_pickle is None: return None try: obj = pickle.loads(obj_pickle) except: # logger.debug("Error decoding pickle") obj = None return obj
def getMemCache(nRequest, nDictCode, nDateTimeType, nDateTimeValue): if nRequest != None: if nRequest.method != "GET": return {'status': another_method_error} nRootConn = nRequest.GET.get('root', '') if len(nRootConn) == 0: nRootConn = 'pNull' else: nRootConn = 'pNull' try: global host if host == 'server': client = Client((gMemCachedUrl), serializer=json_serializer, deserializer=json_deserializer) except Exception: return {'status': traceback.format_exc()} try: context = {} need_update = False if host == 'server': context = client.get(nDictCode) if context is None: need_update = True else: last_updated_datetime = datetime.strptime( context['updated_datetime'], '%Y-%m-%d %H:%M:%S') need_update = needUpdate(nRootConn, last_updated_datetime, nDateTimeType, nDateTimeValue) else: need_update = True if need_update: if host == 'server': if context is not None: if nDictCode in ['INDEX_ONLINE_OPERATORS']: updated_datetime = datetime.today() context[ 'updated_datetime'] = updated_datetime.strftime( '%Y-%m-%d %H:%M:%S') client.set(nDictCode, context) context = getData(nRequest, nDictCode) updated_datetime = datetime.today() context['updated_datetime'] = updated_datetime.strftime( '%Y-%m-%d %H:%M:%S') if host == 'server': client.set(nDictCode, context) except Exception: context = {'status': traceback.format_exc()} if host == 'server': client.close() return context