def test_incr_decr(client_class, host, port, socket_module): client = Client((host, port), socket_module=socket_module) client.flush_all() result = client.incr(b'key', 1, noreply=False) assert result is None result = client.set(b'key', b'0', noreply=False) assert result is True result = client.incr(b'key', 1, noreply=False) assert result == 1 def _bad_int(): client.incr(b'key', b'foobar') with pytest.raises(MemcacheClientError): _bad_int() result = client.decr(b'key1', 1, noreply=False) assert result is None result = client.decr(b'key', 1, noreply=False) assert result == 0 result = client.get(b'key') assert result == b'0'
def test_serialization_deserialization(host, port, socket_module): def _ser(key, value): return json.dumps(value).encode('ascii'), 1 def _des(key, value, flags): if flags == 1: return json.loads(value.decode('ascii')) return value client = Client((host, port), serializer=_ser, deserializer=_des, socket_module=socket_module) client.flush_all() value = {'a': 'b', 'c': ['d']} client.set(b'key', value) result = client.get(b'key') assert result == value
def flush_memcached(): c = Client(("memcached", "11211")) c.flush_all()
def test_misc(client_class, host, port, socket_module): client = Client((host, port), socket_module=socket_module) client.flush_all()
def clearcache(): mc = Client(('cloudassgn.hw0lsb.0001.use2.cache.amazonaws.com', 11211)) mc.flush_all() return 'Flushed the cache...'
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
class MemcachedAdapter(BaseAdapter): """ Exposes a cache store using Memcached. Exposes `pymemcache`'s exceptions. """ def __init__(self, host='localhost', port=11211, **kwargs): super().__init__() self.store = Client((host, port), **kwargs) def set(self, key, value, ttl): if ttl == -1: ttl = 0 return self.store.set(key, value, expire=ttl) def batch_set(self, keys, values, ttls): # There's two reasons to recode pymemcache.set_multi(): # - It returns a list of keys that failed to be inserted, and the base expects a boolean # - It only allows a unique ttl for all keys commands = [] ttls = [0 if ttl == -1 else ttl for ttl in ttls] for key, value, ttl in zip(keys, values, ttls): ttl = self.store._check_integer(ttl, 'expire') # pylint: disable=protected-access key = self.store.check_key(key) value, flags = self.store.serde.serialize(key, value) command = b'set ' + key command += b' ' + str(flags).encode(self.store.encoding) command += b' ' + ttl command += b' ' + str(len(value)).encode( self.store.encoding) + b'\r\n' command += value.encode(self.store.encoding) + b'\r\n' commands.append(command) results = self.store._misc_cmd(commands, 'set', False) # pylint: disable=protected-access for line in results: if line == b'NOT_STORED': return False return True def get(self, key): value = self.store.get(key) return value def batch_get(self, keys): key_to_value = self.store.get_multi(keys) values = [ key_to_value[key] if key in key_to_value else None for key in keys ] return values def delete(self, key): return self.store.delete(key, noreply=False) def batch_delete(self, keys): # Here as well, pymemcache.delete_multi() always returns True commands = [] for key in keys: key = self.store.check_key(key) command = b'delete ' + key + b'\r\n' commands.append(command) results = self.store._misc_cmd(commands, 'delete', False) # pylint: disable=protected-access for line in results: print(f"\"{line}\"") if line == b'NOT_FOUND': return False return True def exists(self, key): # Can't just cast to bool since we can store falsey values return self.store.get(key) is not None def flush(self): return self.store.flush_all(noreply=False) def ping(self): return bool(self.store.stats()) @property def connection_exceptions(self): return (MemcacheUnexpectedCloseError, MemcacheServerError, MemcacheUnknownError)
def memcached(self, host, port): memcached = Client((host, port)) yield memcached memcached.flush_all()
import ssl import utils from pymemcache.client.base import Client # without TLS client = Client(("localhost", 11211)) client.flush_all() # with TLS ctx = ssl.create_default_context(cafile="tls/ca-root.crt") # uncomment for client auth # ctr.load_cert_chain("tls/client.crt", "tls/client.key") tls_client = Client(("localhost", 11212), tls_context=ctx) tls_client.flush_all() def pymemcache_fibonacci(n): utils.validate_input(n) if n in (1, 2): return 1 f = client.get("p" + str(n)) if f is None: f = pymemcache_fibonacci(n - 1) + pymemcache_fibonacci(n - 2) client.set("p" + str(n), str(f))
def on_get(self, req, resp, quote=None): resp.status = falcon.HTTP_200 resp.content_type = 'text/html' picked_quote = pick_quote(quote) if picked_quote == None: resp.status = falcon.HTTP_301 resp.set_header('Location', '/dice') else: context = picked_quote context.update({ 'live_url': LIVE_URL, 'og_image_url': pick_og_image() }) resp.body = load_template('quote.html', context) memcached.flush_all() sslify = FalconSSLify() DEV = os.environ.get('SARRI_DEV', 0) if DEV: log.error(DEV) app = falcon.API() else: app = falcon.API() app.add_route('/', MainResource()) app.add_route('/dice', QuoteResource()) app.add_route('/dice/{quote}', QuoteResource()) app = StaticMiddleware(app, static_root='static', static_dirs=STATIC_DIRS)
from pymemcache.client.base import Client if __name__ == '__main__': client = Client(('172.26.12.92', 11211)) client.flush_all() # client.set('some_key', 'some_value') # result = client.get('some_key') # print(client.get('euler$cache.euler_data_view.321965446094127104')) # client.delete('dlp$cache.dlp_arithmetic_env.10000') # kyes = ['dlp$cache.dlp_arithmetic_env.10000', 'dlp$cache.dlp_arithmetic_env.10001'] # client.delete_many(kyes) # print(result) print("ok")