示例#1
0
def test_errors(host, port, socket_module):
    client = Client((host, port), socket_module=socket_module)
    client.flush_all()

    def _key_with_ws():
        client.set(b'key with spaces', b'value', noreply=False)

    with pytest.raises(MemcacheIllegalInputError):
        _key_with_ws()

    def _key_too_long():
        client.set(b'x' * 1024, b'value', noreply=False)

    with pytest.raises(MemcacheClientError):
        _key_too_long()

    def _unicode_key_in_set():
        client.set(six.u('\u0FFF'), b'value', noreply=False)

    with pytest.raises(MemcacheClientError):
        _unicode_key_in_set()

    def _unicode_key_in_get():
        client.get(six.u('\u0FFF'))

    with pytest.raises(MemcacheClientError):
        _unicode_key_in_get()

    def _unicode_value_in_set():
        client.set(b'key', six.u('\u0FFF'), noreply=False)

    with pytest.raises(MemcacheClientError):
        _unicode_value_in_set()
示例#2
0
def test_quit():
    client = Client(None)
    client.sock = MockSocket([])
    result = client.quit()
    tools.assert_equal(result, None)
    tools.assert_equal(client.sock, None)
    tools.assert_equal(client.buf, '')
示例#3
0
def test_set_success():
    client = Client(None)
    client.sock = MockSocket(['STORED\r\n'])
    result = client.set('key', 'value', noreply=False)
    tools.assert_equal(result, 'STORED')
    tools.assert_equal(client.sock.closed, False)
    tools.assert_equal(len(client.sock.send_bufs), 1)
示例#4
0
def test_stats_conversions():
    client = Client(None)
    client.sock = MockSocket([
        # Most stats are converted to int
        'STAT cmd_get 2519\r\n',
        'STAT cmd_set 3099\r\n',

        # Unless they can't be, they remain str
        'STAT libevent 2.0.19-stable\r\n',

        # Some named stats are explicitly converted
        'STAT hash_is_expanding 0\r\n',
        'STAT rusage_user 0.609165\r\n',
        'STAT rusage_system 0.852791\r\n',
        'STAT slab_reassign_running 1\r\n',
        'STAT version 1.4.14\r\n',
        'END\r\n',
    ])
    result = client.stats()
    tools.assert_equal(client.sock.send_bufs, [
        'stats \r\n'
    ])
    expected = {
        'cmd_get': 2519,
        'cmd_set': 3099,
        'libevent': '2.0.19-stable',
        'hash_is_expanding': False,
        'rusage_user': 0.609165,
        'rusage_system': 0.852791,
        'slab_reassign_running': True,
        'version': '1.4.14',
    }
    tools.assert_equal(result, expected)
def run_test():
    c = Client(("localhost", 11211))
    start = time()
    for x in range(lim):
        c.set(str(uuid1()), "1")

    return time()-start
示例#6
0
def test_errors(host, port):
    client = Client((host, port))
    client.flush_all()

    def _key_with_ws():
        client.set('key with spaces', 'value', noreply=False)

    tools.assert_raises(MemcacheIllegalInputError, _key_with_ws)

    def _key_too_long():
        client.set('x' * 1024, 'value', noreply=False)

    tools.assert_raises(MemcacheClientError, _key_too_long)

    def _unicode_key_in_set():
        client.set(u'\u0FFF', 'value', noreply=False)

    tools.assert_raises(MemcacheClientError, _unicode_key_in_set)

    def _unicode_key_in_get():
        client.get(u'\u0FFF')

    tools.assert_raises(MemcacheClientError, _unicode_key_in_get)

    def _unicode_value_in_set():
        client.set('key', u'\u0FFF', noreply=False)

    tools.assert_raises(MemcacheClientError, _unicode_value_in_set)
示例#7
0
def test_get_error():
    client = Client(None)
    client.sock = MockSocket(['ERROR\r\n'])

    def _get():
        client.get('key')

    tools.assert_raises(MemcacheUnknownCommandError, _get)
示例#8
0
def test_set_server_error():
    client = Client(None)
    client.sock = MockSocket(['SERVER_ERROR some message\r\n'])

    def _set():
        client.set('key', 'value', noreply=False)

    tools.assert_raises(MemcacheServerError, _set)
示例#9
0
def test_set_unicode_value():
    client = Client(None)
    client.sock = MockSocket([''])

    def _set():
        client.set('key', u'\u0FFF', noreply=False)

    tools.assert_raises(MemcacheIllegalInputError, _set)
示例#10
0
def test_stats_with_args():
    client = Client(None)
    client.sock = MockSocket(['STAT fake_stats 1\r\n', 'END\r\n'])
    result = client.stats('some_arg')
    tools.assert_equal(client.sock.send_bufs, [
        'stats some_arg\r\n'
    ])
    tools.assert_equal(result, {'fake_stats': 1})
示例#11
0
def test_get_unicode_key():
    client = Client(None)
    client.sock = MockSocket([''])

    def _get():
        client.get(u'\u0FFF')

    tools.assert_raises(MemcacheIllegalInputError, _get)
示例#12
0
def test_get_unknown_error():
    client = Client(None)
    client.sock = MockSocket(['foobarbaz\r\n'])

    def _get():
        client.get('key')

    tools.assert_raises(MemcacheUnknownError, _get)
示例#13
0
def test_set_error():
    client = Client(None)
    client.sock = MockSocket(['ERROR\r\n'])

    def _set():
        client.set('key', 'value', noreply=False)

    tools.assert_raises(MemcacheUnknownCommandError, _set)
示例#14
0
def test_set_unknown_error():
    client = Client(None)
    client.sock = MockSocket(['foobarbaz\r\n'])

    def _set():
        client.set('key', 'value', noreply=False)

    tools.assert_raises(MemcacheUnknownError, _set)
示例#15
0
def test_incr_exception():
    client = Client(None)
    client.sock = MockSocket([Exception('fail')])

    def _incr():
        client.incr('key', 1)

    tools.assert_raises(Exception, _incr)
    tools.assert_equal(client.sock, None)
    tools.assert_equal(client.buf, '')
示例#16
0
def test_set_many_exception():
    client = Client(None)
    client.sock = MockSocket(['STORED\r\n', Exception('fail')])

    def _set():
        client.set_many({'key' : 'value', 'other' : 'value'}, noreply=False)

    tools.assert_raises(Exception, _set)
    tools.assert_equal(client.sock, None)
    tools.assert_equal(client.buf, '')
示例#17
0
def test_delete_exception():
    client = Client(None)
    client.sock = MockSocket([Exception('fail')])

    def _delete():
        client.delete('key', noreply=False)

    tools.assert_raises(Exception, _delete)
    tools.assert_equal(client.sock, None)
    tools.assert_equal(client.buf, '')
示例#18
0
def test_serialization():
    def _ser(value):
        return json.dumps(value), 0

    client = Client(None, serializer=_ser)
    client.sock = MockSocket(['STORED\r\n'])
    client.set('key', {'a': 'b', 'c': 'd'})
    tools.assert_equal(client.sock.send_bufs, [
        'set key 0 0 20 noreply\r\n{"a": "b", "c": "d"}\r\n'
    ])
示例#19
0
class OnionMemcached(OnionCache):
  def __init__(self, host=('localhost', 11211)):
      from pymemcache.client import Client
      self.memcached_client = Client(host, serializer=json_serializer, deserializer=json_deserializer)

  def get(self, query, params):
    return self.memcached_client.get(key_serializer(query,params))

  def set(self, query, params, document):
    return self.memcached_client.set(key_serializer(query,params),document)
示例#20
0
def test_set_exception():
    client = Client(None)
    client.sock = MockSocket([Exception('fail')])

    def _set():
        client.set('key', 'value', noreply=False)

    tools.assert_raises(Exception, _set)
    tools.assert_equal(client.sock, None)
    tools.assert_equal(client.buf, '')
示例#21
0
def test_incr_decr(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'
示例#22
0
class OnionMemcached(OnionCache):
  def __init__(self, host=('localhost', 11211)):
    try:
      from pymemcache.client import Client
      self.memcached_client = Client(host, serializer=json_serializer, deserializer=json_deserializer)
    except ImportError:
      raise DependencyError("Error importing pymemcache library for OnionMemcached")

  def get(self, query, params):
    return self.memcached_client.get(key_serializer(query,params))

  def set(self, query, params, document):
    return self.memcached_client.set(key_serializer(query,params),document)
示例#23
0
 def _create_client(self):
     """
     Create a new client using smart_client to get connection details
     """
     log.debug("Creating new memcached client")
     try:
         client = Client(
             (self._host, self._port),
             serializer=self._serializer,
             deserializer=self._deserializer,
             no_delay=True,
             ignore_exc=True,
         )
         client._connect()
     except:
         log.debug("Unable to connect to memcached!", exc_info=True)
         raise
     return client
示例#24
0
def delete_test(host, port, socket_module):
    client = Client((host, port), socket_module=socket_module)
    client.flush_all()

    result = client.delete(b'key', noreply=False)
    tools.assert_equal(result, False)

    result = client.get(b'key')
    tools.assert_equal(result, None)
    result = client.set(b'key', b'value', noreply=False)
    tools.assert_equal(result, True)
    result = client.delete(b'key', noreply=False)
    tools.assert_equal(result, True)
    result = client.get(b'key')
    tools.assert_equal(result, None)
示例#25
0
def delete_test(host, port):
    client = Client((host, port))
    client.flush_all()

    result = client.delete('key', noreply=False)
    tools.assert_equal(result, 'NOT_FOUND')

    result = client.get('key')
    tools.assert_equal(result, None)
    result = client.set('key', 'value', noreply=False)
    tools.assert_equal(result, 'STORED')
    result = client.delete('key', noreply=False)
    tools.assert_equal(result, 'DELETED')
    result = client.get('key')
    tools.assert_equal(result, None)
示例#26
0
def test_delete(host, port, socket_module):
    client = Client((host, port), socket_module=socket_module)
    client.flush_all()

    result = client.delete(b'key', noreply=False)
    assert result is False

    result = client.get(b'key')
    assert result is None
    result = client.set(b'key', b'value', noreply=False)
    assert result is True
    result = client.delete(b'key', noreply=False)
    assert result is True
    result = client.get(b'key')
    assert result is None
示例#27
0
def delete_test(host, port):
    client = Client((host, port))
    client.flush_all()

    result = client.delete('key', noreply=False)
    tools.assert_equal(result, False)

    result = client.get('key')
    tools.assert_equal(result, None)
    result = client.set('key', 'value', noreply=False)
    tools.assert_equal(result, True)
    result = client.delete('key', noreply=False)
    tools.assert_equal(result, True)
    result = client.get('key')
    tools.assert_equal(result, None)
示例#28
0
def cas_test(host, port):
    client = Client((host, port))
    client.flush_all()

    result = client.cas('key', 'value', '1', noreply=False)
    tools.assert_equal(result, 'NOT_FOUND')

    result = client.set('key', 'value', noreply=False)
    tools.assert_equal(result, 'STORED')

    result = client.cas('key', 'value', '1', noreply=False)
    tools.assert_equal(result, 'EXISTS')

    result, cas = client.gets('key')
    tools.assert_equal(result, 'value')

    result = client.cas('key', 'value1', cas, noreply=False)
    tools.assert_equal(result, 'STORED')

    result = client.cas('key', 'value2', cas, noreply=False)
    tools.assert_equal(result, 'EXISTS')
示例#29
0
def test_cas(host, port, socket_module):
    client = Client((host, port), socket_module=socket_module)
    client.flush_all()

    result = client.cas(b'key', b'value', b'1', noreply=False)
    assert result is None

    result = client.set(b'key', b'value', noreply=False)
    assert result is True

    result = client.cas(b'key', b'value', b'1', noreply=False)
    assert result is False

    result, cas = client.gets(b'key')
    assert result == b'value'

    result = client.cas(b'key', b'value1', cas, noreply=False)
    assert result is True

    result = client.cas(b'key', b'value2', cas, noreply=False)
    assert result is False
示例#30
0
def cas_test(host, port, socket_module):
    client = Client((host, port), socket_module=socket_module)
    client.flush_all()

    result = client.cas(b'key', b'value', b'1', noreply=False)
    tools.assert_equal(result, None)

    result = client.set(b'key', b'value', noreply=False)
    tools.assert_equal(result, True)

    result = client.cas(b'key', b'value', b'1', noreply=False)
    tools.assert_equal(result, False)

    result, cas = client.gets(b'key')
    tools.assert_equal(result, b'value')

    result = client.cas(b'key', b'value1', cas, noreply=False)
    tools.assert_equal(result, True)

    result = client.cas(b'key', b'value2', cas, noreply=False)
    tools.assert_equal(result, False)
示例#31
0
def application(_environ, start_response):
    """Do Fun things"""
    headers = [("Content-Type", "application/vnd.geo+json")]
    start_response("200 OK", headers)
    mckey = "/geojson/huc12.geojson"
    mc = Client(["iem-memcached", 11211])
    res = mc.get(mckey)
    if res is None:
        res = do()
        mc.set(mckey, res, 86400)
    else:
        res = res.decode("utf-8")
    mc.close()
    return [res.encode("ascii")]
示例#32
0
    def test_memcached_storage_access_token(self):
        if platform.system() == 'Windows':
            return

        from pymemcache.client import Client
        from wechatpy.session.memcachedstorage import MemcachedStorage

        servers = ("127.0.0.1", 11211)
        memcached = Client(servers)
        session = MemcachedStorage(memcached)
        client = WeChatClient(self.app_id, self.secret, session=session)
        with HTTMock(wechat_api_mock):
            token = client.fetch_access_token()
            self.assertEqual('1234567890', token['access_token'])
            self.assertEqual(7200, token['expires_in'])
            self.assertEqual('1234567890', client.access_token)
示例#33
0
def test_serialization_deserialization(host, port):
    def _ser(key, value):
        return json.dumps(value), 1

    def _des(key, value, flags):
        if flags == 1:
            return json.loads(value)
        return value

    client = Client((host, port), serializer=_ser, deserializer=_des)
    client.flush_all()

    value = {'a': 'b', 'c': ['d']}
    client.set('key', value)
    result = client.get('key')
    tools.assert_equal(result, value)
示例#34
0
def delete_value(user_input=None,
                 client=Client((HOST, PORT)),
                 db_name=DATABASE):
    """deletes the value from memcached and sqlite"""
    if user_input is None:
        return False

    response = client.delete(user_input[1])
    try:
        db_conn = KeyValueHelper(db_name)
        del db_conn[user_input[1]]
        db_conn.close()
    except KeyError as error:
        LOG.info('%s not found', user_input[1])
        LOG.error(error)
        response = False

    return response
示例#35
0
class BackendMemcachedCache():
    def __init__(self,
                 host='192.168.24.138',
                 port=11211,
                 timeout=5,
                 ignore_exc=True):
        self.rhost = host
        self.rport = port
        self.rcli = Client((host, port),
                           timeout=timeout,
                           ignore_exc=ignore_exc)

    def put(self, k, v):
        try:
            return self.rcli.set(k, v)
        except BaseException, e:
            logging.exception(e)
            return None
示例#36
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
示例#37
0
class Dao:
    def __init__(self, host, port):
        self.db = Client(
            (host, port),
            default_noreply=False,
            serializer=self.__json_serializer,
            deserializer=self.__json_deserializer
        )

    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.decode('utf-8')
        if flags == 2:
            return json.loads(value.decode('utf-8'))
        raise Exception('Unknown serialization format')

    def get_count(self):
        count = self.db.get('count')
        return int(count) if count else 0

    def get_sushi_list(self, count):
        ids = range(1, count+1)
        result = self.db.get_multi([f'sushi:{id}' for id in ids])
        return [{'id': id, **result[f'sushi:{id}']} for id in ids]

    def incr_count(self):
        count = self.db.get('count')
        if count:
            return self.db.incr('count', 1)
        else:
            self.db.set('count', 1)
            return 1

    def set_sushi(self, sushi):
        self.db.set('sushi:'+str(sushi['id']), sushi)
示例#38
0
def application(environ, start_response):
    """Do Fun things"""
    headers = [("Content-Type", "application/vnd.geo+json")]
    start_response("200 OK", headers)
    form = parse_formvars(environ)
    cb = form.get("callback", None)
    huc12 = form.get("huc12", "000000000000")[:12]
    mode = form.get("mode", "daily")

    mckey = "/geojson/huc12_events/%s/%s" % (huc12, mode)
    mc = Client(["iem-memcached", 11211])
    res = mc.get(mckey)
    if res is None:
        res = do(huc12, mode)
        mc.set(mckey, res, 15)
    else:
        res = res.decode("utf-8")
    mc.close()
    if cb is not None:
        res = "%s(%s)" % (cb, res)
    return [res.encode("ascii")]
示例#39
0
def main(environ):
    """Do something fun"""
    form = parse_formvars(environ)
    year = form.get("year", 2015)
    month = form.get("month", 5)
    day = form.get("day", 5)
    year2 = form.get("year2", year)
    month2 = form.get("month2", month)
    day2 = form.get("day2", day)
    scenario = int(form.get("scenario", 0))
    v = form.get("v", "avg_loss")
    huc = form.get("huc")

    ts = datetime.date(int(year), int(month), int(day))
    ts2 = datetime.date(int(year2), int(month2), int(day2))
    mckey = "/auto/map.py/%s/%s/%s/%s/%s" % (
        huc,
        ts.strftime("%Y%m%d"),
        ts2.strftime("%Y%m%d"),
        scenario,
        v,
    )
    if form.get("overview"):
        mckey = "/auto/map.py/%s/%s" % (huc, form.get("zoom"))
    mc = Client(["iem-memcached", 11211])
    res = mc.get(mckey)
    if res is None:
        # Lazy import to help speed things up
        if form.get("overview"):
            res, do_cache = make_overviewmap(form)
        else:
            res, do_cache = make_map(huc, ts, ts2, scenario, v, form)
        sys.stderr.write("Setting cache: %s\n" % (mckey,))
        if do_cache:
            mc.set(mckey, res, 3600)
    mc.close()
    return res
示例#40
0
def test_touch_found():
    client = Client(None)
    client.sock = MockSocket(['TOUCHED\r\n'])
    result = client.touch('key', noreply=False)
    tools.assert_equal(result, True)
 def make_client(self, mock_socket_values, serializer=None):
     client = Client(None, serializer=serializer)
     client.sock = MockSocket(list(mock_socket_values))
     return client
示例#42
0
def test_stats_with_args():
    client = Client(None)
    client.sock = MockSocket(['STAT fake_stats 1\r\n', 'END\r\n'])
    result = client.stats('some_arg')
    tools.assert_equal(client.sock.send_bufs, ['stats some_arg\r\n'])
    tools.assert_equal(result, {'fake_stats': 1})
示例#43
0
def test_misc(host, port, socket_module):
    client = Client((host, port), socket_module=socket_module)
    client.flush_all()
示例#44
0
 def __init__(self, host, port, timeout):
     self.host = host
     self.port = port
     self.client = Client((self.host, self.port), connect_timeout=timeout, timeout=timeout, socket_module=gevent.socket)
# coding=utf-8
#import memcache
from collections import defaultdict
import json

from distance_scoring.scoring_doc import scoring_distance
from pymemcache.client import Client
mc = Client(("127.0.0.1", 12122))
mc.flush_all()
#def save_model_phi(filepath):
#    with open(filepath, "r") as file_obj:
#        for i, line in enumerate(file_obj):
#            for j, score in enumerate(line.split()):
#                score = float(score)
#                mc.set("phi%s_%s"%(i,j), score)


def save_model_nw(filepath):

    nw_sum = defaultdict(int)
    with open(filepath, "r") as file_obj:
        word_topic_dict = defaultdict(dict)
        for word_idx, line in enumerate(file_obj):
            for topic_idx, count in enumerate(line.split()):
                count = int(count)
                nw_sum[topic_idx] += count
                if count > 0:
                    word_topic_dict[word_idx][topic_idx] = count
        for word_idx, topic_count_dict in word_topic_dict.iteritems():
            mc.set("nw%s" % (word_idx), topic_count_dict)
    for topic_idx, count in nw_sum.iteritems():
示例#46
0
def test_append_prepend(host, port, socket_module):
    client = Client((host, port), socket_module=socket_module)
    client.flush_all()

    result = client.append(b'key', b'value', noreply=False)
    assert result is False
    result = client.get(b'key')
    assert result is None

    result = client.set(b'key', b'value', noreply=False)
    assert result is True
    result = client.append(b'key', b'after', noreply=False)
    assert result is True
    result = client.get(b'key')
    assert result == b'valueafter'

    result = client.prepend(b'key1', b'value', noreply=False)
    assert result is False
    result = client.get(b'key1')
    assert result is None

    result = client.prepend(b'key', b'before', noreply=False)
    assert result is True
    result = client.get(b'key')
    assert result == b'beforevalueafter'
示例#47
0
def test_touch_not_found():
    client = Client(None)
    client.sock = MockSocket(['NOT_FOUND\r\n'])
    result = client.touch('key', noreply=False)
    tools.assert_equal(result, False)
示例#48
0
def test_get_set(host, port, socket_module):
    client = Client((host, port), socket_module=socket_module)
    client.flush_all()

    result = client.get('key')
    assert result is None

    client.set(b'key', b'value', noreply=False)
    result = client.get(b'key')
    assert result == b'value'

    client.set(b'key2', b'value2', noreply=True)
    result = client.get(b'key2')
    assert result == b'value2'

    result = client.get_many([b'key', b'key2'])
    assert result == {b'key': b'value', b'key2': b'value2'}

    result = client.get_many([])
    assert result == {}
 def make_client(self, mock_socket_values, serializer=None):
     mock_client = Client(None, serializer=serializer, key_prefix=b'xyz:')
     mock_client.sock = MockSocket(list(mock_socket_values))
     client = PooledClient(None, serializer=serializer, key_prefix=b'xyz:')
     client.client_pool = pool.ObjectPool(lambda: mock_client)
     return client
    def test_socket_connect(self):
        server = ("example.com", 11211)

        client = Client(server, socket_module=MockSocketModule())
        client._connect()
        tools.assert_equal(client.sock.connections, [server])

        timeout = 2
        connect_timeout = 3
        client = Client(server,
                        connect_timeout=connect_timeout,
                        timeout=timeout,
                        socket_module=MockSocketModule())
        client._connect()
        tools.assert_equal(client.sock.timeouts, [connect_timeout, timeout])

        client = Client(server, socket_module=MockSocketModule())
        client._connect()
        tools.assert_equal(client.sock.socket_options, [])

        client = Client(server,
                        socket_module=MockSocketModule(),
                        no_delay=True)
        client._connect()
        tools.assert_equal(client.sock.socket_options,
                           [(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)])
示例#51
0
def test_incr_noreply():
    client = Client(None)
    client.sock = MockSocket([])
    result = client.incr('key', 1, noreply=True)
    tools.assert_equal(result, None)
示例#52
0
def test_decr_found():
    client = Client(None)
    client.sock = MockSocket(['1\r\n'])
    result = client.decr('key', 1, noreply=False)
    tools.assert_equal(result, 1)
示例#53
0
def test_add_replace(host, port, socket_module):
    client = Client((host, port), socket_module=socket_module)
    client.flush_all()

    result = client.add(b'key', b'value', noreply=False)
    assert result is True
    result = client.get(b'key')
    assert result == b'value'

    result = client.add(b'key', b'value2', noreply=False)
    assert result is False
    result = client.get(b'key')
    assert result == b'value'

    result = client.replace(b'key1', b'value1', noreply=False)
    assert result is False
    result = client.get(b'key1')
    assert result is None

    result = client.replace(b'key', b'value2', noreply=False)
    assert result is True
    result = client.get(b'key')
    assert result == b'value2'
示例#54
0
def test_delete_noreply():
    client = Client(None)
    client.sock = MockSocket([])
    result = client.delete('key', noreply=True)
    tools.assert_equal(result, True)
示例#55
0
class MemcachedStore(object):
    """Caching implementation that uses Memcached as data storage.

    :param host: String representation of hostname or IP of the memcached server

    :param port: Port number (int) on which the memcached server is listening

    :param connect_timeout: optional float, seconds to wait for a connection to
        the memcached server. Defaults to "forever" (uses the underlying
        default socket timeout, which can be very long)

    :param timeout: optional float, seconds to wait for send or recv calls on
        the socket connected to memcached. Defaults to "forever" (uses the
        underlying default socket timeout, which can be very long).

    :param no_delay: optional bool, set the TCP_NODELAY flag, which may help
        with performance in some cases. Defaults to False.

    :param ignore_exc: optional bool, True to cause the "get", "gets",
        "get_many" and "gets_many" calls to treat any errors as cache
        misses. Defaults to True. Ie. if the cache is failing use the
        Stormpath API.

    :param socket_module: socket module to use, e.g. gevent.socket. Defaults to
        the standard library's socket module.

    :param key_prefix: Prefix of key. You can use this as namespace. Defaults
        to b''.

    """

    DEFAULT_TTL = 5 * 60  # seconds

    def __init__(self,
                 host='localhost',
                 port=11211,
                 connect_timeout=None,
                 timeout=None,
                 no_delay=False,
                 ignore_exc=True,
                 key_prefix=b'',
                 socket_module=socket,
                 ttl=DEFAULT_TTL):
        self.ttl = ttl

        try:
            from pymemcache.client import Client as Memcache
        except ImportError:
            raise RuntimeError(
                'Memcached support is not available. Run "pip install pymemcache".'
            )

        self.memcache = Memcache((host, port),
                                 serializer=json_serializer,
                                 deserializer=json_deserializer,
                                 connect_timeout=connect_timeout,
                                 timeout=timeout,
                                 socket_module=socket_module,
                                 no_delay=no_delay,
                                 ignore_exc=ignore_exc,
                                 key_prefix=key_prefix)

    @memcache_error_handling
    def __getitem__(self, key):
        entry = self.memcache.get(key)

        if entry is None:
            return None

        return CacheEntry.parse(entry)

    @memcache_error_handling
    def __setitem__(self, key, entry):
        self.memcache.set(key, entry, expire=self.ttl)

    @memcache_error_handling
    def __delitem__(self, key):
        self.memcache.delete(key)

    @memcache_error_handling
    def clear(self):
        self.memcache.flush_all()

    @memcache_error_handling
    def __len__(self):
        return self.memcache.stats()['curr_items']
示例#56
0
def test_flush_all():
    client = Client(None)
    client.sock = MockSocket(['OK\r\n'])
    result = client.flush_all(noreply=False)
    tools.assert_equal(result, True)
def get_connection(host, port):
    client = Client((host, port),
                    serializer=json_serializer,
                    deserializer=json_deserializer)
    client._connect()
    return client
示例#58
0
def test_get_recv_chunks():
    client = Client(None)
    client.sock = MockSocket(
        ['VALUE key', ' 0 5\r', '\nvalue', '\r\n', 'END', '\r', '\n'])
    result = client.get('key')
    tools.assert_equal(result, 'value')
示例#59
0
import requests
import re
from pymemcache.client import Client
from xml.dom.minidom import parseString
import sys

# User defined variables
feedaddr = 'http://malwaredb.malekal.com/export.php?type=url'
feedID = 'malekal_malware'
mkey = 'fetcher_malekal_malware:feeddata'
killchain = 'Malware'

# No user modifications needed below.
client = Client(('localhost', 11211))
result = client.get(mkey)

if isinstance(result, str):
    dom = parseString(result)
else:
    r = requests.get(feedaddr)
    client.set(mkey, r.content, expire=7200)
    dom = parseString(r.content)

xmlroot = dom.getElementsByTagName("uris")[0]
print('url,feedID,killchain,description,method')

for entry in xmlroot.getElementsByTagName("url"):
    url = entry.getElementsByTagName('address')[0].firstChild
    if url is None: continue
    url = url.nodeValue.strip()
    msplit = url.split('://')
示例#60
0
def test_delete_found():
    client = Client(None)
    client.sock = MockSocket(['DELETED\r\n'])
    result = client.delete('key', noreply=False)
    tools.assert_equal(result, True)