class MCStore(object): def __init__(self, server): self.server = server self.mc = Client([server]) def __repr__(self): return '<MCStore(server=%s)>' % repr(self.server) def __str__(self): return self.server def reopen(self): self.mc = Client([self.server]) def set(self, key, data, rev=0): return bool(self.mc.set(key, data, rev)) def get(self, key): return self.mc.get(key) def get_multi(self, keys): return self.mc.get_multi(keys) def delete(self, key): return bool(self.mc.delete(key))
class MemcacheLM: def __init__(self, dont_load_info=False): #from memcache import Client from cmemcached import Client #from cmemcache import Client location = ['127.0.0.1:11211'] #location = ['127.0.0.1:21201'] #location = ['127.0.0.1:13000'] self.mc = Client(location) if dont_load_info: self.info = {'big_n': 0} else: self.load_info() self.counts = { 'unigram': MemcacheDictInterface(self.mc, 'unigram'), 'bigram': MemcacheDictInterface(self.mc, 'bigram'), } def add(self, type, ngram): # ignore type. key = "_".join(ngram) key = binascii.b2a_base64(key)[:-1] #key = (type,ngram) #key = mc_keymaker((type,ngram)) c = self.mc.get(key) if c is None: self.mc.set(key, 0) self.mc.incr(key) def load_info(self, filename='background_model.info.json'): self.info = simplejson.loads(open(filename).read()) def save_info(self, filename='background_model.info.json'): f = open(filename, 'w') simplejson.dump(self.info, f) f.close()
def __init__(self, dont_load_info=False): #from memcache import Client from cmemcached import Client #from cmemcache import Client location = ['127.0.0.1:11211'] #location = ['127.0.0.1:21201'] #location = ['127.0.0.1:13000'] self.mc = Client(location) if dont_load_info: self.info = {'big_n': 0} else: self.load_info() self.counts = { 'unigram': MemcacheDictInterface(self.mc, 'unigram'), 'bigram': MemcacheDictInterface(self.mc, 'bigram'), }
def paste_lexer(pastename,lexmode=None): global data, formatter memory = Client(memcached) database = Client(memcachedb) bottle.response.content_type = "text/html;charset=UTF-8" if bottle.request.environ['HTTP_X_FORWARDED_SSL'] == 'on': proto = 'https' else: proto = 'http' paste = msgpack.loads(get_data(memory,database,"paste:"+pastename) or "") if paste: if lexmode: url="%s://%s/p%s/%s"%(proto,domain,pastename,lexmode) else: url="%s://%s/p%s"%(proto,domain,pastename) rurl="%s://%s/rp%s"%(proto,domain,pastename) if not paste[4]: lexer_name = (lexmode or paste[2]).lower() try: lexer = get_lexer_by_name(lexer_name, stripall=True) except: lexer = get_lexer_by_name('text', stripall=True) hlpaste = memory.get("hlcache:%s:%s" % (pastename, lexer_name)) if hlpaste == None: hlpaste = highlight(paste[0].decode("utf-8"), lexer, formatter) memory.set("hlcache:%s:%s" % (pastename, lexer_name), hlpaste.encode("utf-8")) else: hlpaste = hlpaste.decode("utf-8") return bottle.template('paste', paste=hlpaste, title=paste[1], url=url, rurl=rurl, passwd=None, lexmode=lexer.name) else: if 'p' in bottle.request.params: if sha256(bottle.request.params['p']).digest() == paste[4]: lexer_name = (lexmode or paste[2]).lower() try: lexer = get_lexer_by_name(lexer_name, stripall=True) except: lexer = get_lexer_by_name('text', stripall=True) hlpaste = memory.get("hlcache:%s:%s" % (pastename, lexer_name)) if hlpaste == None: hlpaste = highlight(paste[0].decode("utf-8"), lexer, formatter) memory.set("hlcache:%s:%s" % (pastename, lexer_name), hlpaste.encode("utf-8")) else: hlpaste = hlpaste.decode("utf-8") return bottle.template('paste', paste=hlpaste, title=paste[1], url=url, rurl=rurl, passwd=bottle.request.params['p'], lexmode=lexer.name) else: return bottle.template("pasteauth", url=url, rurl=rurl, passwd=None, incorrect=True) else: return bottle.template("pasteauth", url=url, rurl=rurl, passwd=None, incorrect=False) else: raise HTTPError(404, "Paste not in database")
class MemcacheLM: def __init__(self, dont_load_info=False): # from memcache import Client from cmemcached import Client # from cmemcache import Client location = ["127.0.0.1:11211"] # location = ['127.0.0.1:21201'] # location = ['127.0.0.1:13000'] self.mc = Client(location) if dont_load_info: self.info = {"big_n": 0} else: self.load_info() self.counts = { "unigram": MemcacheDictInterface(self.mc, "unigram"), "bigram": MemcacheDictInterface(self.mc, "bigram"), } def add(self, type, ngram): # ignore type. key = "_".join(ngram) key = binascii.b2a_base64(key)[:-1] # key = (type,ngram) # key = mc_keymaker((type,ngram)) c = self.mc.get(key) if c is None: self.mc.set(key, 0) self.mc.incr(key) def load_info(self, filename="background_model.info.json"): self.info = simplejson.loads(open(filename).read()) def save_info(self, filename="background_model.info.json"): f = open(filename, "w") simplejson.dump(self.info, f) f.close()
def __init__(self, dont_load_info=False): # from memcache import Client from cmemcached import Client # from cmemcache import Client location = ["127.0.0.1:11211"] # location = ['127.0.0.1:21201'] # location = ['127.0.0.1:13000'] self.mc = Client(location) if dont_load_info: self.info = {"big_n": 0} else: self.load_info() self.counts = { "unigram": MemcacheDictInterface(self.mc, "unigram"), "bigram": MemcacheDictInterface(self.mc, "bigram"), }
def __init__(self, server): self.server = server self.mc = Client([server], do_split=0)
class MCStore(object): def __init__(self, server): self.server = server self.mc = Client([server], do_split=0) def __del__(self): self.close() def __repr__(self): return '<MCStore(server=%s)>' % repr(self.server) def __str__(self): return self.server def set(self, key, data, rev=0): return bool(self.mc.set(key, data, rev)) def set_raw(self, key, data, rev=0, flag=0): # interface for sync data if rev < 0: raise str(rev) return self.mc.set_raw(key, data, rev, flag) def get(self, key): try: r = self.mc.get(key) if r is None and self.mc.get_last_error() != 0: raise IOError(self.mc.get_last_error()) return r except ValueError: self.mc.delete(key) def get_raw(self, key): r, flag = self.mc.get_raw(key) if r is None and self.mc.get_last_error() != 0: raise IOError(self.mc.get_last_error(), self.mc.get_last_strerror()) return r, flag def get_multi(self, keys): return self.mc.get_multi(keys) def delete(self, key): return bool(self.mc.delete(key)) def close(self): if self.mc is not None: self.mc.close() self.mc = None
class MCStore(object): def __init__(self, server): self.server = server self.mc = Client([server], do_split=0) def __del__(self): self.close() def __repr__(self): return '<MCStore(server=%s)>' % repr(self.server) def __str__(self): return self.server def set(self, key, data, rev=0): return bool(self.mc.set(key, data, rev)) def set_raw(self, key, data, rev=0, flag=0): # interface for sync data if rev < 0: raise str(rev) return self.mc.set_raw(key, data, rev, flag) def get(self, key): try: r = self.mc.get(key) if r is None and self.mc.get_last_error() != 0: raise IOError(self.mc.get_last_error()) return r except ValueError: self.mc.delete(key) def get_raw(self, key): r, flag = self.mc.get_raw(key) if r is None and self.mc.get_last_error() != 0: raise IOError( self.mc.get_last_error(), self.mc.get_last_strerror()) return r, flag def get_multi(self, keys): return self.mc.get_multi(keys) def delete(self, key): return bool(self.mc.delete(key)) def close(self): if self.mc is not None: self.mc.close() self.mc = None
def raw_mc(memcached): return Client([memcached])
def reopen(self): self.mc = Client([self.server])
def __init__(self, server): self.server = server self.mc = Client([server])
def prefixed_mc(memcached): return Client([memcached], prefix=PREFIX)