def import_preferred_memcache_lib(self, servers): """Returns an initialized memcache client. Used by the constructor.""" try: import pylibmc except ImportError: pass else: return pylibmc.Client(servers) try: from google.appengine.api import memcache except ImportError: pass else: return memcache.Client() try: import memcache except ImportError: pass else: return memcache.Client(servers)
def __init__(self, servers, debug=False, noreply=False, no_block=False, min_compress_len=512 * 1024, num_clients=10): self.servers = servers self.clients = pylibmc.ClientPool(n_slots=num_clients) for x in xrange(num_clients): client = pylibmc.Client(servers, binary=True) behaviors = { 'no_block': no_block, # use async I/O 'tcp_nodelay': True, # no nagle '_noreply': int(noreply), 'ketama': True, # consistent hashing } client.behaviors.update(behaviors) self.clients.put(client) self.min_compress_len = min_compress_len
def clear_database() -> None: # Hacky function only for use inside populate_db. Designed to # allow running populate_db repeatedly in series to work without # flushing memcached or clearing the database manually. # With `zproject.test_settings`, we aren't using real memcached # and; we only need to flush memcached if we're populating a # database that would be used with it (i.e. zproject.dev_settings). if default_cache['BACKEND'] == 'django_pylibmc.memcached.PyLibMCCache': pylibmc.Client( [default_cache['LOCATION']], behaviors=default_cache[ "OPTIONS"] # type: ignore # settings not typed properly ).flush_all() model = None # type: Any # Hack because mypy doesn't know these are model classes for model in [ Message, Stream, UserProfile, Recipient, Realm, Subscription, Huddle, UserMessage, Client, DefaultStream ]: model.objects.all().delete() Session.objects.all().delete()
def __init__(self, servers=None, default_timeout=300, key_prefix=None, username=None, password=None, **kwargs): super(SASLMemcachedCache, self).__init__(default_timeout=default_timeout) if servers is None: servers = ["127.0.0.1:11211"] import pylibmc self._client = pylibmc.Client(servers, username=username, password=password, binary=True, **kwargs) self.key_prefix = key_prefix
def client(request, host, port): if request.param == "pylibmc": if not HAS_PYLIBMC: pytest.skip("requires pylibmc") client = pylibmc.Client(['{0}:{1}'.format(host, port)]) client.behaviors = {"tcp_nodelay": True} elif request.param == "memcache": if not HAS_MEMCACHE: pytest.skip("requires python-memcached") client = memcache.Client(['{0}:{1}'.format(host, port)]) elif request.param == "pymemcache": if not HAS_PYMEMCACHE: pytest.skip("requires pymemcache") client = pymemcache.client.Client((host, port)) else: pytest.skip("unknown library {0}".format(request.param)) client.flush_all() return client
def init(): for i in range(8): caches.append([ pylibmc.Client(["192.168.1.64:11211"], binary=False, behaviors={ "tcp_nodelay": True, "ketama": True, "connect_timeout": 200 }), pylibmc.Client(["192.168.1.173:11211"], binary=False, behaviors={ "tcp_nodelay": True, "ketama": True, "connect_timeout": 200 }), pylibmc.Client(["192.168.1.233:11211"], binary=False, behaviors={ "tcp_nodelay": True, "ketama": True, "connect_timeout": 200 }), pylibmc.Client(["192.168.1.87:11211"], binary=False, behaviors={ "tcp_nodelay": True, "ketama": True, "connect_timeout": 200 }), pylibmc.Client(["192.168.1.152:11211"], binary=False, behaviors={ "tcp_nodelay": True, "ketama": True, "connect_timeout": 200 }), pylibmc.Client(["192.168.1.213:11211"], binary=False, behaviors={ "tcp_nodelay": True, "ketama": True, "connect_timeout": 200 }) ])
def XYToEL(NL, X, Y, XY, IJ): cache = mc.Client() r = _r(XY) if not cache.get('XYToEL.elementIndex') == None: elementIndex = cache.get('XYToEL.elementIndex') else: elementIndex = np.array( flatter( np.array([element[1][:, 1].reshape(1, -1) for element in IJ]))) cache.set('XYToEL.elementIndex', elementIndex) elementLables = np.argwhere((elementIndex[:, 0] == NL) + (elementIndex[:, 1] == NL) + (elementIndex[:, 2] == NL) + (elementIndex[:, 3] == NL)) elementLables = flatter(elementLables) # [SW, SE, NW, NE] _xi = X - XY[NL][1] _eta = Y - XY[NL][2] if _xi <= 0 and _eta <= 0: elementLable = elementLables[0] elif _xi > 0 and _eta < 0: elementLable = elementLables[1] elif _xi < 0 and _eta > 0: elementLable = elementLables[2] elif _xi > 0 and _eta > 0: elementLable = elementLables[3] else: elementLable = elementLables[0] return elementLable
def handle(self, *args, **options): client = mc.Client(['127.0.0.1:11211']) self.stdout.write('start flush') appId = getSetting('admin', 'app_id') appSecret = getSetting('admin', 'app_secret') component_verify_ticket = getSetting('admin', 'component_verify_ticket') postUrl = ( "https://api.weixin.qq.com/cgi-bin/component/api_component_token") data = '''{ "component_appid":"%s" , "component_appsecret": "%s", "component_verify_ticket": "%s" }''' % (appId, appSecret, component_verify_ticket) resp = urllib.urlopen(postUrl, data).read() urlResp = json.loads(resp) logger.info('flush cron: urlResp %s' % urlResp) if 'component_access_token' not in urlResp: print resp accessToken = urlResp['component_access_token'] saveSetting('admin', 'component_access_token', accessToken) client.delete('xunhui__admin_config_admin_component_access_token')
def index(request): # whether we have new project result result = -1 if request.GET.get('result'): result = int(request.GET.get('result')) # try to get the current like count count = 527 # default is this value, just for fun if environ.get("APP_NAME", ""): # if online, get like count from memcache import pylibmc as memcache mc = memcache.Client() if not mc.get('count'): mc.set("count", "527") count = mc.get("count") # get some projects to show projects = Project.objects.filter(state=True).order_by('id') page = request.GET.get('page') projects = _paginator(request, projects, page=page, size=PROJECTS_PER_PAGE) return render(request, 'shan/index.html', { 'projects': projects, 'result': result, 'count': count })
def create_instance(cls): if hasattr(cls, '_instance'): return cls._instance if hasattr(options, 'memcache_clients') and options.memcache_clients: try: import pylibmc cache = pylibmc.Client(options.memcache_clients, **options.memcache_kwargs) cls._instance = cache return cls._instance except ImportError: import memcache cache = memcache.Client(options.memcache_clients, **options.memcache_kwargs) cls._instance = cache return cls._instance except ImportError: cls._instance = cls() return cls._instance else: cls._instance = cls() return cls._instance
def __init__(self, creep): self.admins = [] self.bucket = boto3.resource('s3').Bucket(os.environ['S3_BUCKET_NAME']) self.password = os.environ['QUOTE_DELETE_PASSWORD'] self.cache = {} try: raise Exception('disable memcache') credentials = json.loads( os.environ['VCAP_SERVICES'] )['memcachier'][0]['credentials'] self.memcached = pylibmc.Client( credentials['servers'].split(','), binary=True, username=credentials['username'], password=credentials['password'], behaviors={ "tcp_nodelay": True, "ketama": True, "no_block": True, } ) except: self.memcached = None
def GET_cachehealth(self): results = {} behaviors = { # Passed on to poll(2) in milliseconds "connect_timeout": 1000, # Passed on to setsockopt(2) in microseconds "receive_timeout": int(1e6), "send_timeout": int(1e6), } for server in cache._CACHE_SERVERS: try: if server.startswith("udp:"): # libmemcached doesn't support UDP get/fetch operations continue mc = pylibmc.Client([server], behaviors=behaviors) # it's ok that not all caches are mcrouter, we'll just ignore # the miss either way mc.get("__mcrouter__.version") results[server] = "OK" except pylibmc.Error as e: g.log.warning("Health check for %s FAILED: %s", server, e) results[server] = "FAILED %s" % e return json.dumps(results)
def start_matchmaking(username, user_profile, game, level_match_buffer, use_voice_chat): memcache_client = memcache.Client([MEMCACHE_SERVER], binary=True, username=MEMCACHE_USERNAME, password=MEMCACHE_PASSWORD) match_session = Match(memcache_client.get(game)) matched_username, matched_profile = match_session.find_match( user_profile, game, level_match_buffer, use_voice_chat) game_session = None if not matched_profile: user_profile['use_voice_chat'] = use_voice_chat match_session.add_user_to_session(username, user_profile) else: game_session = { username: user_profile, matched_username: matched_profile } if not memcache_client.set(game, match_session.get_session()): raise Exception("Failed to update matchmaking session") return game_session
class TestingConfig(Config): TESTING = True SECRET_KEY = '\x9f\x04\xb4\x8a\x14\xb9\xd5Pn\xa0\xb4\xfe\xc3\xfdi\xfdn\xe28{\xd5\xc7\xcb\xde' CACHE_CONFIG = pylibmc.Client(["127.0.0.1"], binary=True, behaviors={ # Faster IO "tcp_nodelay": True, # Keep connection alive 'tcp_keepalive': True, # Timeout for set/get requests 'connect_timeout': 2000, # ms 'send_timeout': 750 * 1000, # us 'receive_timeout': 750 * 1000, # us '_poll_timeout': 2000, # ms # Better failover 'ketama': True, 'remove_failed': 1, 'retry_timeout': 2, 'dead_timeout': 30, })
def __init__(self, roidb, num_loaders=4, minibatch_queue_size=64, blobs_queue_capacity=8): self._mc = pylibmc.Client(["127.0.0.1:11212"], binary=True, behaviors={ "tcp_nodelay": True, "ketama": True }) self._roidb = roidb self._lock = threading.Lock() self._perm = deque(range(len(self._roidb))) self._cur = 0 # _perm cursor # The minibatch queue holds prepared training data in host (CPU) memory # When training with N > 1 GPUs, each element in the minibatch queue # is actually a partial minibatch which contributes 1 / N of the # examples to the overall minibatch self._minibatch_queue = Queue.Queue(maxsize=minibatch_queue_size) self._blobs_queue_capacity = blobs_queue_capacity # Random queue name in case one instantiates multple RoIDataLoaders self._loader_id = uuid.uuid4() self._blobs_queue_name = 'roi_blobs_queue_{}'.format(self._loader_id) # Loader threads construct (partial) minibatches and put them on the # minibatch queue self._num_loaders = num_loaders self._num_gpus = cfg.NUM_GPUS self.coordinator = Coordinator() self._output_names = get_minibatch_blob_names() self._shuffle_roidb_inds() self.create_threads()
def create_recommendation_microservice(self, recommender_folder, memcache_servers=None, memcache_pool_size=2): """ create recommedation Flask microservice app Parameters ---------- recommender_folder : str location of recommender model files memcache_servers : comma separated string, optional memcache server locations, e.g., 127.0.0.1:11211 memcache_pool_size : int, optional size of memcache pool """ app = Flask(__name__) if not memcache_servers is None: mc = pylibmc.Client(memcache_servers) _mc_pool = pylibmc.ClientPool(mc, memcache_pool_size) app.config["seldon_memcache"] = _mc_pool if self.aws_key: rw = seldon.RecommenderWrapper(aws_key=self.aws_key, aws_secret=self.aws_secret) else: rw = seldon.RecommenderWrapper() recommender = rw.load_recommender(recommender_folder) app.config["seldon_recommender"] = recommender app.register_blueprint(recommend_blueprint) # other setup tasks return app
def __init__(self, whoami_uri, secret_key=None, cache_servers=None, cache_prefix="ProxyCacheUser/", cache_timeout=60 * 60, **kw): self.proxy = ProxyUser(whoami_uri) # Use a randomly-generated secret key if none is specified. # This is secure, but will reduce the re-usability of the cache. if secret_key is None: secret_key = os.urandom(32) elif len(secret_key) < 32: raise ValueError("secret_key should be at least 256 bit") self.hmac_master = hmac.new(secret_key, "", hashlib.sha256) # Default to memcached running on localhost if no server specified. if isinstance(cache_servers, str): cache_servers = [cache_servers] elif cache_servers is None: cache_servers = ['127.0.0.1:11211'] self.cache_prefix = cache_prefix self.cache_timeout = int(cache_timeout) self.cache_client = pylibmc.Client(cache_servers) self.cache_pool = BottomlessClientPool(self.cache_client)
def __init__(self, *args, **kwargs): """ Args: loglevel: loglevel expiration_seconds: key expiration time servers: memcached servers key_prefix: cache key prefix """ self.log = logging.getLogger(__name__) self.expiration_seconds = kwargs.get("expiration_seconds") self.servers = kwargs.get("servers", DEFAULT_SERVERS) self.key_prefix = kwargs.get("key_prefix", "mc") loglevel = kwargs.get("loglevel", logging.CRITICAL) self.log.setLevel(loglevel) if self.expiration_seconds: try: self.expiration_seconds = int(self.expiration_seconds) except (TypeError, ValueError): self.expiration_seconds = DEFAULT_EXPIRATION_SECONDS else: self.expiration_seconds = DEFAULT_EXPIRATION_SECONDS self._mc = pylibmc.Client(self.servers)
def POST(self): str_xml = web.data() #获得post来的数据 xml = etree.fromstring(str_xml)#进行XML解析 # content=xml.find("Content").text#获得用户所输入的内容 msgType=xml.find("MsgType").text fromUser=xml.find("FromUserName").text toUser=xml.find("ToUserName").text mc = pylibmc.Client() #初始化一个memcache实例用来保存用户的操作 memcache它可以应对任意多个连接,使用非阻塞的网络IO #关注事件的提醒 if msgType == "event": mscontent = xml.find("Event").text if mscontent == "subscribe": replayText = u'''欢迎关注本微信,哈哈~!在这里我会不定期推送有关大数据、云计算方面的内容。\n功能列表:1.输入t+中文或者英文返回对应的英中翻译\n2.输入【m】随机来首音乐听,建议在wifi下听\n3.输入【Ly+你的留言内容】,来给我留言\n4.【博客地址】 http://blog.csdn.net/zwto1\n 5.【查阅书籍信息】输入书籍名,便可查看相关书籍信息。\n\n你可以【查看历史消息】来看已发布过的自己感兴趣的内容,也可以输入【menu】去探索我开发的一些功能。 ''' return self.render.reply_text(fromUser,toUser,int(time.time()),replayText) if mscontent == "unsubscribe": replayText = u'我现在功能还很简单,知道满足不了您的需求,但是我会慢慢改进,欢迎您以后再来' return self.render.reply_text(fromUser,toUser,int(time.time()),replayText) #help指令的识别 if msgType == 'text': content=xml.find("Content").text if content.lower() == 'menu': replayText = u'''1.输入t+中文或者英文返回对应的英中翻译\n2.输入【m】随机来首音乐听,建议在wifi下听\n3.输入【Ly+你的留言内容】,来给我留言\n4.【博客地址】 http://blog.csdn.net/zwto1\n 5.【查阅书籍信息】输入书籍名,便可查看相关书籍信息\n\n 不要忘记你可以【查看历史消息】来看已发布过的自己感兴趣的内容哟^_^''' #3.输入【xhj】进入调戏小黄鸡模式\n return self.render.reply_text(fromUser,toUser,int(time.time()),replayText) #音乐随机播放 if content.lower() == 'm': musicList = [ [r'','Destiny',u'献给虫虫'], # r 后加音乐链接 [r'','5 Days',u'献给虫虫'], [r'','Far Away (Album Version)',u'献给虫虫'], [r'',u'少年游',u'献给虫虫'], ] music = random.choice(musicList) musicurl = music[0] musictitle = music[1] musicdes =music[2] return self.render.reply_music(fromUser,toUser,int(time.time()),musictitle,musicdes,musicurl) if content.startswith('ly'): fktime = time.strftime('%Y-%m-%d %H:%M',time.localtime()) db.addfk(fromUser,fktime,content[2:].encode('utf-8')) return self.render.reply_text(fromUser,toUser,int(time.time()),u'感谢你的留言') elif type(content).__name__ == "unicode": content = content.encode('UTF-8') if(content.startswith('t')): Nword = youdao(content[1:]) return self.render.reply_text(fromUser,toUser,int(time.time()),Nword) else: book = query_book(content) if book == '': return self.render.reply_text(fromUser,toUser,int(time.time()),u'对不起,你查找的图书不存在!') else: # description = query_book_details() book_title = book["title"] book_img = book["images"]["large"] description = book["summary"] book_alt = book['alt'] return self.render.reply_tw(fromUser,toUser,int(time.time()), book_title, description,book_img, book_alt)
def __init__(self, server, params): super(CacheClass, self).__init__(params) mc = pylibmc.Client(server.split(';')) mc.behaviors = PYLIBMC_BEHAVIORS self._pool = pylibmc.ThreadMappedPool(mc) self._logger = CacheLogger()
import pytest from pytest_lazyfixture import lazy_fixture pymemcache_client = pymemcache.client.Client(('127.0.0.1', 11211)) pythonmemcache_client = memcache.Client(["127.0.0.1:11211"]) redis_py_client = redis.StrictRedis() try: import pylibmc except ImportError: pylibmc_client = None else: pylibmc_client = pylibmc.Client(['127.0.0.1']) class StorageDict(dict): pass @pytest.fixture def storage_dict(): storage = StorageDict() storage.ring = ring.dict storage.is_binary = False storage.has_has = True storage.has_touch = True storage.has_expire = True return storage
def __init__(self, *args, **kwargs): self.pool = kwargs.pop('pool', None) or ThreadPoolExecutor(1) self.mc = pylibmc.Client(*args, **kwargs) self.mc_pool = pylibmc.ThreadMappedPool(self.mc)
def interact(servers, banner=banner, binary=False): mc = pylibmc.Client(servers, binary=binary) local = {"pylibmc": pylibmc, "mc": mc} code.interact(banner=banner, local=local)
import pylibmc import time mc = pylibmc.Client(['127.0.0.1:11211']) mc["some_key"] = "Some value" mc["some_key"] mc["another_key"] = 3 mc["another_key"] del mc["another_key"] "some_key" in mc mc["another_key"] = 9 mc.set("key", 1) # note that the key used for incr/decr must be a string. value = mc.get("key") print(value) mc.incr("key") value = mc.get("key") print(value) mc.decr("key") value = mc.get("key") print(value) mc.get_multi(["key", "another_key"]) mc.set_multi({"cats": ["on acid", "furry"], "dogs": True}) mc.get_multi(["cats", "dogs"]) mc.delete_multi(["cats", "dogs", "nonextant"]) sample_obj = {"name": "Soliman", "lang": "Python"} mc.set("sample_user", sample_obj, time=15)
import pwd import os import datetime from tornado import gen import tornado.web import pylibmc as memcache from lib.base_httphandler import BaseHandler USER = pwd.getpwuid(os.getuid())[0] if USER == 'liutaihua': # for my private secret from same_spider.secret_liutaihua import header else: from same_spider.secret import header mc = memcache.Client(['127.0.0.1:11211']) class MainHandler(tornado.web.RequestHandler): def get(self): self.set_header('Access-Control-Allow-Origin', '*') #return self.finish({'res': "衙门严打,临时停服"}) return self.render('index-test.html') class NotFoundPage(BaseHandler): def get(self): return self.finish("美女全跑光了(此频道20点后开放), 没找到任何东西...") class SortSensesHandler(tornado.web.RequestHandler): def get(self): self.set_header('Access-Control-Allow-Origin', '*')
import os import random import subprocess import sys import time import celery import pylibmc import remoulade from remoulade.brokers.rabbitmq import RabbitmqBroker from remoulade.brokers.redis import RedisBroker logger = logging.getLogger("example") counter_key = "latench-bench-counter" memcache_client = pylibmc.Client(["localhost"], binary=True) memcache_pool = pylibmc.ClientPool(memcache_client, 8) random.seed(1337) if os.getenv("REDIS") == "1": broker = RedisBroker() remoulade.set_broker(broker) celery_app = celery.Celery(broker="redis:///") else: broker = RabbitmqBroker(host="127.0.0.1") remoulade.set_broker(broker) celery_app = celery.Celery(broker="amqp:///") def fib_bench(n):
You should have received a copy of the GNU General Public License along with Proofchecker. If not, see <http://www.gnu.org/licenses/>. """ import requests import json import hashlib import pylibmc from time import time from .htmlparsing import get_search_text, get_github_text, get_twitter_url from .sites import SITES from .config import MEMCACHED_PORT, MEMCACHED_TIMEOUT, DEFAULT_HOST, MEMCACHED_ENABLED mc = pylibmc.Client([DEFAULT_HOST + ':' + str(MEMCACHED_PORT)], binary=True) def contains_valid_proof_statement(search_text, username): search_text = search_text.lower() verification_styles = [ "verifying myself: my bitcoin username is +%s" % username, "verifying myself: my bitcoin username is %s" % username, "verifying myself: my openname is %s" % username, "verifying that +%s is my bitcoin username" % username, "verifying that %s is my bitcoin username" % username, "verifying that %s is my openname" % username, "verifying that +%s is my openname" % username, "verifying i am +%s on my passcard" % username, "verifying that +%s is my blockchain id" % username
#-*-coding:utf-8-*- # anthor: Xiang xiyun # date: 2014-11-28 # describe: This file is used to get the access_token regularly from wechat import sae.const #引入sae的常量 import urllib2 import json import pylibmc as memcache mc = memcache.Client() token = mc.get("token") if token == None: appid = "wxe1abdf23e3b46c20" secret = "4c4c57e68e0e1552f18fb22f23e2a29d" url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + appid + '&secret=' + secret response = urllib2.urlopen(url) html = response.read() tokeninfo = json.loads(html) token = tokeninfo['access_token'] #set access_token into memcache, do not compress, expires_in 7100 seconds incase error mc.set("token", token, 0, 7100) token = mc.get("token")
# -*- coding: utf-8 -*- from setting import * #Memcache mc = None try: import pylibmc mc = pylibmc.Client() mc.set('check_memcache_available', '1', 3600) print 'memcache is available!!' except: mc = None print 'can not use memcache!!' # 读取缓存 def get_cache(key): print 'key=%s' % key if not mc or not key: return None content = mc.get(str(key)) print 'content=%s' % content return content # 缓存 def set_cache(key, value, time=cache_time): if not mc or not key: return mc.set(key, value, time)
def __init__(self,params): self.timeout = params.get('timeout', 3600) host = params.get('host', ['localhost']) self.client = pylibmc.Client(host, binary=True)