class CHIDriverMemcache(CHIDriver):
    """Драйвер Memcache."""
    def __init__(self, *av, **kw):
        """Конструктор."""
        super().__init__(*av, **kw)

        self.client = Memcache(
            (self.server[0]["host"], self.server[0]["port"]),
            connect_timeout=self.connect_timeout,
            timeout=self.request_timeout,
        )

    def driver_set(self, key, packed_chi_object, ttl):
        """Переопределение для установки драйвера - в редисе используются две команды, а тут - одна."""
        self.client.set(key, packed_chi_object, ttl)

    def keys(self, mask):
        """Возвращает ключи по маске."""
        raise CHIMethodIsNotSupportedException(
            "Метод keys для мемкеша не поддерживается.")

    def erase(self, mask):
        """Удаление ключей по маске."""
        raise CHIMethodIsNotSupportedException(
            "Метод erase для мемкеша не поддерживается.")
Ejemplo n.º 2
0
    def run(self):

        client = Client(('127.0.0.1', 11211))
        url = 'https://talosintelligence.com/documents/ip-blacklist'
        talosvalue = 'talos-ip'

        try:
            response = requests.get(url)
            if (response):
                responseArr = []
                for line in response.text.splitlines():
                    if (line.startswith('#') == False):
                        responseArr.append(line)
                valueCheck = client.get_many(responseArr)
                for k in responseArr:
                    valueArr = []
                    tempArr = []
                    tempArr.append(talosvalue)
                    if k in valueCheck:
                        val = valueCheck[k].decode()
                        valueArr = talosToMemcache.stringHelper(val)
                        for item in valueArr:
                            if item not in tempArr:
                                tempArr.append(item)
                    client.set(k, tempArr, 300)

        except Exception as e:
            with open('/var/log/misppullLog.txt', 'a') as file:
                file.write(
                    '{0} - talosFeed-script failed with error: {1} \n'.format(
                        str(time.asctime()), str(e)))
Ejemplo n.º 3
0
class Client:
    def __init__(self, config):
        self.config = config
        self.memcache_client = PymemcacheClient(server=(self.config["ip"], self.config["port"]), serializer=self.json_serializer, deserializer=self.json_deserializer, connect_timeout=self.config["connect_timeout"], timeout=self.config["timeout"])

    def json_serializer(self, key, value):
        if type(value) == str:
            return value, 1
        return json.dumps(value), 2

    def json_deserializer(self, key, value, flags):
        if flags == 1:
            return value
        if flags == 2:
            return json.loads(value)
        raise Exception("Unknown serialization format")

    def write(self, key, message):
        logger.info("Writing to cache: {}".format(key))
        self.memcache_client.set(key,
                                 message,
                                 expire=self.config["key_expiration"],
                                 noreply=self.config["noreply_flag"])

    def read(self, key):
        logger.info("Reading from cache: {}".format(key))
        return self.memcache_client.get(key)
Ejemplo n.º 4
0
class NoSQLCache:
    __instance = None
    timeout = 5

    @staticmethod
    def inst():
        if NoSQLCache.__instance == None:
            NoSQLCache.__instance = NoSQLCache(SqliteDb.inst())
        return NoSQLCache.__instance

    def __init__(self, sql_db_object):
        self.client = Client(('localhost', 11211))
        self.sql_db = sql_db_object

    def get(self, key):
        key_corrector = re.sub(r' ', '$', key)
        value = self.client.get(key_corrector)
        if value:
            return eval(value.decode('utf-8'))
        else:
            value = self.sql_db.get(key)
            self.set(key_corrector, value, self.timeout)
            return value

    def set(self, key, value, timeout):
        self.client.set(key, value, expire=timeout)
class MemcachedWrapper():

    MEMCACHED_CONNECT_TIMEOUT = 5  # 5 seconds
    MEMCACHED_TIMEOUT = 5  # 5 seconds

    def __init__(self, memcached_location):

        memcached_location_portions = memcached_location.split(":")
        if len(memcached_location_portions) != 2:
            raise ValueError(
                f"Found incorrectly formatted parameter memcached location: {memcached_location}"
            )

        memcached_host = memcached_location_portions[0]
        memcached_port = int(memcached_location_portions[1])

        self.memcached_client = Client(
            server=(memcached_host, memcached_port),
            serializer=serde.python_memcache_serializer,
            deserializer=serde.python_memcache_deserializer,
            connect_timeout=MemcachedWrapper.MEMCACHED_CONNECT_TIMEOUT,
            timeout=MemcachedWrapper.MEMCACHED_TIMEOUT)

    def get(self, key):
        return self.memcached_client.get(key)

    def set(self, key, value, timeout=None):
        self.memcached_client.set(key, value, timeout)

    def delete(self, key):
        self.memcached_client.delete(key)
def execqueryWithMem(name, c1, c2):
    try:
        mysql_conn = mysql.connector.connect(host=host, user=dbusername, password=dbpassword, database=dbname,
                                             port=port)
        mc = Client(('cloudassgn.hw0lsb.0001.use2.cache.amazonaws.com', 11211))

        query = 'select ' + name + ' from Earthquake where ' + c1 + ' and ' + c2
        hashVal = hashlib.sha224(query).hexdigest()

        starttime = int(round(time.time() * 1000))

        data = mc.get(hashVal)
        count = 0

        if not data:
            for i in range(0, 250):
                cursor = mysql_conn.cursor()
                cursor.execute(query)
                row = cursor.fetchall()
                count = cursor.rowcount
                cursor.close()
            mc.set(hashVal, count)

        endtime = int(round(time.time() * 1000))
        totalexectime = endtime - starttime

        mysql_conn.close()

        resultStr = '<div style="font-size:14px;margin-top: 30px;"><div> Last Name : Manakan </div><div> Last 4 digit ID : 6131 </div><div> Class Section : 10:30 AM </div></div>'
        resultStr = resultStr + '<br> Time taken : ' + str(totalexectime) + ' msecs'
        resultStr = resultStr + '<br> Rows effected : ' + str(count)
        return resultStr
    except Exception as e:
        print e
        return 'Error ' + str(e)
Ejemplo n.º 7
0
class Cache:
    client = None
    __path = ""
    __md5 = ""
    __chunks = []

    def __init__(self, chunk_size=1000000):
        self.client = Client(('localhost', 11211))
        self.__chunk_size = chunk_size

    def readFile(self, path, data=None):
        self.__path = path
        if data is not None:
            data = data.encode()
        self.__md5 = self.__getMd5(data)
        self.__getChunks(data)
        self.__cache()
        return self.__chunks

    def getFile(self, name):
        """ TODO: use join() instead """
        i = 0
        content = bytearray()
        while True:
            data = self.client.get("{}:{}".format(name, i))
            if not data:
                break
            content += data
            i += 1

        checksum = self.client.get("{}:hash".format(name))
        new_md5 = hashlib.md5(content).digest()
        assert checksum == new_md5, 'data corrupted'
        return content

    def __cache(self):
        self.client.set("{}:hash".format(self.__path), self.__md5)
        for i, chunk in enumerate(self.__chunks):
            item_key = "{}:{}".format(self.__path, i)
            self.client.set(item_key, chunk)

    def __getMd5(self, data=None):
        if data is not None:
            return hashlib.md5(data).digest()
        file = open(self.__path, 'rb')
        content = file.read()
        file.close()
        return hashlib.md5(content).digest()

    def __getChunks(self, data=None):
        self.__chunks = []
        if data is not None:
            return hashlib.md5(data).digest()
        file = open(self.__path, 'rb')
        count = 0
        while True:
            data = file.read(self.__chunk_size)
            if not data:
                break
            self.__chunks.append(data)
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
def main():
	line1 = Line(1, 5)
	line2 = Line(2, 6)
	line3 = Line(6, 8)
	line4 = Line(7, 9)
	
	assert do_intersect(line1, line2) == True
	assert do_intersect(line1, line3) == False
	
	assert do_intersect(line2, line1) == True
	assert do_intersect(line3, line1) == False
	
	assert do_intersect(line3, line4) == True
	
	assert compare_versions("1.1", "1.2") == -1
	assert compare_versions("1.2.1", "1.2") == 1
	assert compare_versions("1.2.1", "1.2.1a") == -1
	assert compare_versions("1.2.1b", "1.2.1a") == 1
	assert compare_versions("1.2.1", "1.21") == -1
	assert compare_versions("2.1.1", "1.2.1") == 1
	assert compare_versions("1.2.1", "1.2.1") == 0
	assert compare_versions("1.", "1") == 0
	assert compare_versions("a1", "1a") == 1
	assert compare_versions("1.2.1", "1.19.0") == -1
	
	client = Client('127.0.0.1', 11211)
	client.set('some_key', 'some_value', expire = 30)
	assert client.get('some_key') == 'some_value'
	print("OK")
Ejemplo n.º 10
0
class Reader(BaseReader):
    """
    Memcached settings Reader

    A simple memcached getter using pymemcache library.
    """
    _default_conf = {
        'host': 'localhost',
        'port': 11211
    }

    def __init__(self, conf):
        super(Reader, self).__init__(conf)

        self.client = Client((self.conf['host'], self.conf['port']))

    def _get(self, key):
        result = self.client.get(key)
        if isinstance(result, six.binary_type):
            result = result.decode('utf-8')

        return result

    def _set(self, key, value):
        self.client.set(key, value, noreply=False)
def startClient(id):
    global active_threads
    global max_active_threads

    try:
        client = Client(('localhost', 8888))
        print('Client ' + str(id) + ' connected')
        start = time.time()

        lock.acquire()
        active_threads += 1
        max_active_threads = max(max_active_threads, active_threads)
        lock.release()

        for _ in range(500):
            key, value = randomString(5), randomString(5)
            client.set(key, value)
            result = client.get(key)

        lock.acquire()
        active_threads -= 1
        lock.release()

        end = time.time()
        print('Client ' + str(id) + ' exiting. Time spent: ' +
              str(end - start))
        end = time.time()
    except BaseException as e:
        print('Exception' + str(e))
        return
Ejemplo n.º 12
0
def runOTX():
    '''Retrieve intel from OTXv2 API.'''

    days = int(parser.get('otx', 'days_of_history'))
    key = parser.get('otx', 'api_key')
    mem_host = parser.get('memcached', 'mem_host')
    mem_port = int(parser.get('memcached', 'mem_port'))
    memcached = Client((mem_host, mem_port))
    memcached_agetime = int(parser.get('memcached', 'agetime'))
    memcached_sleeptime = int(parser.get('memcached', 'sleeptime'))
    mtime = (datetime.now() - timedelta(days=days)).isoformat()

    for pulse in iter_pulses(key, mtime):
        pulse_name = pulse['name']
        pulse_id = pulse['id']
        for indicator in pulse[u'indicators']:
            ioc = indicator['indicator']
            ioc_type = map_indicator_type(indicator[u'type'])
            tag = pulse_name + '-' + pulse_id
            if ioc_type is None:
                continue
            try:
                url = pulse[u'references'][0]
            except IndexError:
                url = 'https://otx.alienvault.com'
            memcached_key = ioc_type + '-' + ioc
            try:
                memcached.set(memcached_key.encode('utf-8'),
                              tag.encode('utf-8'), memcached_agetime)
            except:
                pass
    time.sleep(memcached_sleeptime)
Ejemplo n.º 13
0
def UpdateClassifier():
    print("Update Classifier")

    clf = LogisticRegression(
        random_state=0, solver='lbfgs', multi_class='multinomial')
    smt = SMOTETomek(random_state=42)

    discussions = list(Discussion.objects.filter(reviewed=True))
    globalFeaturesIndex = ef.GetGlobalFeaturesIndex(
        discussions, list(range(0, len(discussions))), ef.E19)

    X, y = [], []
    for discussion in discussions:
        featureVector = ef.ExtractFeatureFromCorpus(
            globalFeaturesIndex, discussion.content, ef.E19)
        X.append(featureVector)
        y.append(discussion.tag.tag_id)

    X, y = smt.fit_sample(X, y)
    selector = GenericUnivariateSelect(chi2, 'percentile', param=20)
    X = selector.fit_transform(X, y)

    try:
        clf.fit(X, y)
        print("fit done")
        client = Client(('localhost', 11211))
        model = (clf, globalFeaturesIndex, selector)
        client.set('model', pickle.dumps(model))
        model_in_bytes = client.get('model')
        model_from_cache = pickle.loads(model_in_bytes)
        print(len(model_from_cache))
    except:
        print("clf failed to cache...")
Ejemplo n.º 14
0
def run_island(island):
    import pygmo as pg
    from multiprocessing import cpu_count
    from pymemcache.client.base import Client
    mc_client = Client((island.mc_host, island.mc_port))
    #udp = island.problem_factory()

    algorithm = pg.algorithm(pg.de())
    #problem = pg.problem(udp)
    problem = island.problem_factory()
    # TODO: configure pop size
    i = pg.island(algo=algorithm, prob=problem, size=20)

    mc_client.set(island.domain_qualifier('island', str(island.id), 'status'),
                  'Running', 10000)
    mc_client.set(island.domain_qualifier('island', str(island.id), 'n_cores'),
                  str(cpu_count()), 10000)
    #print('Starting island {} with {} cpus'.format(str(i.id), str(cpu_count())))

    i.evolve()
    i.wait()

    import socket
    hostname = socket.gethostname()
    ip = [
        l for l in ([
            ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]
            if not ip.startswith("127.")
        ][:1], [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close())
                 for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]
                 ][0][1]]) if l
    ][0][0]
    return (ip, hostname, i.get_population().problem.get_fevals())
Ejemplo n.º 15
0
def set_test(request):
    client = Client(('/opt/www/memcached/memcached.sock'),
                    serializer=json_serializer,
                    deserializer=json_deserializer)
    client.set('new', {'status': 'updated'})
    # result = client.get('key')

    return JsonResponse({'status': 'ok'})
Ejemplo n.º 16
0
def adduser(request):
    if request.method == 'POST':
        data = request.get_data()
        json_data = json.loads(data.decode('utf-8'))

        username = json_data.get('username')
        password = json_data.get('password')
        email = json_data.get('email')

        userid = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for i in range(16)
        ])

        if username is None or password is None or email is None:
            return json.dumps({'status': 'error', "error": "have empty block"})

        key = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for i in range(16)
        ])

        timestamp = int(time.time())

        res = dbcontroller.mysql_checkuser(username, email)

        if res == False:
            return json.dumps({
                'status':
                'error',
                'error':
                'email already register or username already in use'
            })

        #use memcache
        mc = Client((memhost, memport))

        info = {
            "username": username,
            "password": password,
            "email": email,
            "timestamp": timestamp,
            "key": key,
            "userid": userid
        }
        print(info)
        mc.set(email, info)
        #return json.dumps({'status':mc.get(email)})

        if sendMail(email, key) == True:
            return json.dumps({'status': 'OK'})
        else:
            return json.dumps({
                'status': 'error',
                'error': 'invalid email address'
            })
    else:
        return json.dumps({'status': 'error'})
Ejemplo n.º 17
0
class Memcached(object):
    """Memcached based caching.
    This is extremely lightweight in terms of memory usage, but it
    appears to be slightly slower than local database caching, and a
    lot slower than local memory based caching. This should be used if
    the cache is to be shared between multiple users.
    """

    LRU = LeastRecentlyUsed = 0

    def __init__(self, url='127.0.0.1', mode=LRU, ttl=None):
        """Create a new engine.

        Parameters:
            url (str): Memcached server to connect to.
            mode (int): How to purge the old keys.
                This does not affect anything as memcached is LRU only.
                The option is there to match the other engines.
            ttl (int): Time the cache is valid for.
                Set to None or 0 for infinite.
        """
        try:
            from pymemcache.client.base import Client
            self.client = Client(url, timeout=3, connect_timeout=3)
        except ImportError:
            from memcache import Client
            self.client = Client([url], socket_timeout=3)
        self.ttl = ttl

        # Don't allow cross version caches
        # Pickle may have incompatibilities between versions
        self._key_prefix = '{}.{}.'.format(sys.version_info.major,
                                           sys.version_info.minor)

    def get(self, key):
        """Get the value belonging to a key.
        An error will be raised if the cache is expired or doesn't
        exist.
        """
        value = self.client.get(self._key_prefix + key)
        if value is None:
            raise exceptions.CacheNotFound(key)
        return _decode(value)

    def put(self, key, value, ttl=None):
        """Write a new value to the cache.
        This will overwrite any old cache with the same key.
        """
        if ttl is None:
            ttl = self.ttl
        self.client.set(self._key_prefix + key,
                        _encode(value),
                        ttl or 0,
                        noreply=True)

    def delete(self, key):
        """Delete an item of cache if it exists."""
        return self.client.delete(self._key_prefix + key, noreply=False)
Ejemplo n.º 18
0
def test_memcached(mc_host, mc_port):
    # test that memcache is working
    from pymemcache.client.base import Client
    mc_client = Client((mc_host, mc_port))
    my_id = str(uuid4())
    my_val = str(uuid4())
    mc_client.set(my_id, my_val, 1000)
    if mc_client.get(my_id).decode('utf8') != my_val:
        raise RuntimeError('Unable to communicate with Memcached')
Ejemplo n.º 19
0
class MemcacheTool(object):
    def __init__(self, host, port):
        self.client = Client((host, port))

    def set(self, key, value):
        self.client.set(key, value)

    def get(self, key):
        return self.client.get(key)
Ejemplo n.º 20
0
def set_memcache(k, data):
    '''将数据加入到缓存中
    '''
    try:
        client = Client(('127.0.0.1', 1121))
        client.set(k, json.dump(data))
        return True
    except Exception as e:
        print(e)
        return False
Ejemplo n.º 21
0
class MemClient():
    def __init__(self):
        self.client = Client(('localhost', 11211))

    def get(self, key):
        value = self.client.get(key)
        return json.loads(value)

    def set(self, key, value):
        self.client.set(key, json.dumps(value), expire=18000)
Ejemplo n.º 22
0
 def memcclient(self, host):
     c = Client(host)
     lock = c.get('lock')
     while lock:
         sleep(.25)
         lock = c.get('lock')
     c.set('lock', 'true')
     try:
         yield c
     finally:
         c.delete('lock')
Ejemplo n.º 23
0
def fib_handler(k):
    client = Client((MEMCACHED_HOST, '11211')
        , serializer=json_serializer
        , deserializer=json_deserializer) # клиент кэширования
    fib_result = client.get(str(k)) # число фибоначчи из кэша
    if fib_result is None:
        result = fib(k)
        client.set(str(k), result)  # кэшируем число фибоначчи
    else: 
        result = fib_result
    print('fib_result=', fib_result)
    return str(result)
Ejemplo n.º 24
0
 def __init__(self, islands, topology, initial_score=None):
     from pymemcache.client.base import Client
     if initial_score is None:
         self.initial_score = ...
     else:
         self.initial_score = initial_score
     self.topology = topology
     self.mc_host = mc_host
     self.mc_port = mc_port
     mc_client = Client((self.mc_host, self.mc_port))
     mc_client.set(self.domain_qualifier('islandIds'),
                   dumps(topology.island_ids), 10000)
Ejemplo n.º 25
0
def fib_handler(num):
    client = Client((MEMCACHED_HOST, '11211'),
                    serializer=json_serializer,
                    deserializer=json_deserializer)
    fib_result = client.get(str(num))
    if fib_result is None:
        result = fib(num)
        client.set(str(num), result)
    else:
        result = fib_result
    print('fib_result=', fib_result)
    return str(result)
Ejemplo n.º 26
0
def memcached_load_nosql(objects, numbers):
    from pymemcache.client.base import Client
    import hashlib

    mc = Client(('localhost', 11211), serializer=mem_serializer, deserializer=mem_deserializer)
    test_name = 'memcached nosql '

    for i, o in enumerate(objects):
        mc.set(str(hashlib.sha1(o["name"]).hexdigest()), o["coords"])
        if (i + 1) in numbers:
            print test_name + "Objects: %s, Usage: %s" % (i + 1, mc.stats()["bytes"] * 1.0 / (1024 * 1024))
    print "finsihed"
Ejemplo n.º 27
0
class MemcachedCache(object):
    """ a memcached cache which stores the states as pickled objects """

    def __init__(self, master):
        self.is_master = False
        self.wait_on_insert = master.wait_on_insert
        self.memc_params = master.memc_params
        self.val_ttl = master.val_ttl

    def __del__(self):
        self.teardown()

    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):
        return self

    def fetch(self, routingkey, key):
        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 insert(self, routingkey, key, obj):
        cache_key = str(routingkey) + '_' + key
        obj_pickle = pickle.dumps(obj)
        if self.wait_on_insert:
            self.client.set(key=cache_key, value=obj_pickle, expire=self.val_ttl)
        else:
            threading.Thread(target=self.client.set,
                             kwargs={'key': cache_key, 'value': obj_pickle, 'expire': self.val_ttl},
                             daemon=False).start()

    def get_client(self):
        return self.__copy__()

    def __copy__(self):
        if not self.is_master:
            raise Exception('not allowed; already a slave')
        return MemcachedCache(self)

    def copy(self):
        return self.__copy__()
Ejemplo n.º 28
0
def set_api_throttle():
	
	client = Client((CACHE_CLIENT_ENDPOINT, CACHE_PORT))	
	result = client.get('daily_requests')
		
	if (result):
		result = int(result) + 1	
	else:
		result = 1
			
	client.set('daily_requests',result, expire=CACHE_EXPIRE)
	
	return result
Ejemplo n.º 29
0
class MemcachedCacheDriver(CacheDriver):
    def init(self):
        self.expiration = None
        self.client = MemClient(('localhost', 11211))

    def get(self, key):
        cache = self.client.get(key)
        if cache is None:
            return None
        return json.loads(cache.decode('utf8'))

    def set(self, key, value):
        self.client.set(key, value, self.expiration)
Ejemplo n.º 30
0
    def set(self, k, data):
        """将数据加入到缓存中
		"""
        logger.info("memcached: %s %s: %s" %
                    (data, self.memcachedendpoint, self.memcachedport))
        try:
            client = Client((self.memcachedendpoint, self.memcachedport))
            client.set(k, data, 300)
            logger.debug(data)
            return True
        except Exception as e:
            logger.info(e)
            return False
Ejemplo n.º 31
0
class MemcachedCache(CachualCache):
    """A cache using `Memcached <https://memcached.org/>`_ as the backing
    cache. The same caveats apply to keys and values as for Redis - you should
    only try to store strings (using the packing/unpacking functions). See the
    documentation on Keys and Values here:
    :class:`pymemcache.client.base.Client`.

    :type host: string
    :param host: The Memcached host to use for the cache.

    :type port: integer
    :param port: The port to use for the Memcached server.

    :type kwargs: dict
    :param kwargs: Any additional args to pass to the :class:`CachualCache`
                   constructor.
    """
    def __init__(self, host='localhost', port=11211, **kwargs):
        super(MemcachedCache, self).__init__(**kwargs)
        self.client = MemcachedClient((host, port))

    def get(self, key):
        """Get a value from the cache using the given key.

        :type key: string
        :param key: The cache key to get the value for.

        :returns: The value for the cache key, or None in the case of cache
                  miss.
        """
        return self.client.get(key)

    def put(self, key, value, ttl=None):
        """Put a value into the cache at the given key. For constraints on keys
        and values, see :class:`pymemcache.client.base.Client`.

        :type key: string
        :param key: The cache key to use for the value.

        :param value: The value to store in the cache.

        :type ttl: integer
        :param ttl: The time-to-live for key in seconds, after which it will
                    expire.
        """
        if ttl is None:
            ttl = 0
        self.client.set(key, value, expire=ttl)
Ejemplo n.º 32
0
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'
Ejemplo n.º 33
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
class Memcached(object):
    DELAY = 0.5
    DEBUG = False

    def __init__(self, hostname, port, **params):
        self.mc = Client((hostname, port))

    def handle(self, topic, message):
        """
        """
        if 'cmd' not in message:
            raise Exception("Bad message: no command")
        cmd = message['cmd']
        if not hasattr(self, cmd):
            raise Exception("Unknown command: " + cmd)
        tryit = True
        while tryit:
            tryit = False
            try:
                getattr(self, cmd)(message)
            except MemcacheUnexpectedCloseError:
                # Server dropped dead - we'll retry
                tryit = True
            except IOError:
                # Something network-related - retry
                tryit = True
            if tryit:
                time.sleep(self.DELAY)

    def set(self, message):
        text = message['val'].encode('utf-8')
        if message.get('sbt', None):
            purge_time = time.time() + message.get('uto', 0)
            text = text.replace('$UNIXTIME$', '%.6f' % purge_time)
        if self.DEBUG:
            print("Set {0}-{1}-{2}".format(message['key'].encode('utf-8'), text, int(message['ttl'])))
        self.mc.set(message['key'].encode('utf-8'), text, int(message['ttl']))

    def delete(self, message):
        self.mc.delete(message['key'])
Ejemplo n.º 35
0
        m, n = getscreenimage()
        return m, n

#------------------------
#logging.basicConfig(
#    format='# %(levelname)s: %(message)s',
#    level=logging.DEBUG,
#)

client = Client(('127.0.0.1', 11211))

try:
        result = client.get('appletv')
        if result:
            sys.exit("time is not passed")
        else:
            print ("appletv is on")
            # get current channel and change input to HDMI2
            # tv should be on
            m, n = getCHandHDMI2()
            if m != n:
                changeinout(m, n)
            else:
                handleCommand("20")
            client.set('appletv', 'true', 60) 

except Exception, e:
        print e.__doc__
        print e.message      
        sys.exit(1)
Ejemplo n.º 36
0
def gao_memcache():
    client = Client((app.config['MEMCACHE_HOST'], app.config['MEMCACHE_PORT']))
    client.set('some_key', 'some_value', expire=10)
    result = client.get('some_key')
    print result
import time
import urllib2
from PIL import Image
from pymemcache.client.base import Client

client = Client(('127.0.0.1', 11211))
url = "http://127.0.0.1:5080/index1.jpg"
i = 0

try:
    while True:
        try:
            input = urllib2.urlopen(url)
            input.readline(); input.readline()
            content_length = int(input.readline().split(": ")[1].strip())
            input.readline(); input.readline()
            data = input.read(content_length)
            client.set(str(i), data, 30)
            client.set('curno', i)
    
            i = i + 1 
            time.sleep(0.7)
    
        except Exception, e:
            print e.__doc__
            print e.message      
            sys.exit(1)

except KeyboardInterrupt:
    sys.exit(1)