Example #1
0
class MemcacheStore(DictProxy):

   def __init__(self, host, port):
      from memcache import Client
      self.mc= Client(['%s:%s' % (host, port)])

   def update(self, updates):
      for update in updates:
         (processId, data)= update
         self.mc.set(processId, data)

   def get(self, processId, default= None):
      data= self.mc.get(processId)
      if data == None:
         return default
      return data

   def pop(self, processId):
      data= self.mc.get(processId)
      self.mc.delete(processId)
      return data
      if data == None:
         return default

   def __len__(self):
      return int(self.mc.get_stats()[0][1].get('curr_items'))
Example #2
0
def preset(sample):

    global addresses
    conn = Client([gethostname()+':11211'])

    if conn.get_stats()[0][1]['curr_items'] == 0:

        for k in addresses:
            conn.set(addr_prefix+k, 0)
Example #3
0
def preset(sample):

    global addresses
    conn = Client([gethostname() + ':11211'])

    if conn.get_stats()[0][1]['curr_items'] == 0:

        for k in addresses:
            conn.set(addr_prefix + k, 0)
Example #4
0
class MemcacheBackend(StorageBackend):
    def __init__(self, options, args):
        if not options.servers:
            raise Exception("memcache servers are required")

        self.servers = options.servers.split(",")

        self.open()

    @classmethod
    def parse_arguments(cls, optparse):
        optparse.add_option(
            "-m",
            "--servers",
            dest="servers",
            help="comma-separated list of memcached servers",
            metavar="SERVERS",
            default="localhost:11211",
        )

    def _encode_key(self, key):
        return key.encode("base64").rstrip("\n")

    def _decode_key(self, key):
        return key.decode("base64")

    def _get(self, key, default=None):
        return self.mc.get(self._encode_key(key))

    def _get_multi(self, keys):
        keys = map(self._encode_key, keys)
        ret = self.mc.get_multi(keys)
        return dict((self._decode_key(key), value) for (key, value) in ret.iteritems())

    def _put(self, key, val):
        return self.mc.set(self._encode_key(key), val)

    def _put_multi(self, keys):
        keys = dict((self._encode_key(key), value) for (key, value) in keys.iteritems())
        self.mc.set_multi(keys)

    def _delete(self, key):
        self.mc.delete(self._encode_key(key))

    def close(self):
        if getattr(self, "mc", None):
            self.mc.disconnect_all()
            self.mc = None

    def open(self):
        self.close()
        self.mc = MemcacheClient(self.servers)

    def stats(self):
        return dict(self.mc.get_stats())
Example #5
0
class MemcacheCli(cmd.Cmd, object):
    def __init__(self, host_list):
        super(MemcacheCli, self).__init__()
        self.memcache = Client(
            host_list)  # Use our memcache library to initiate a connection
        self._check_connection(
            host_list
        )  # Make sure we actually have connections, since our library doesn't make this apparent
        self.prompt = '(memcache) '
        # Time for some hardcore introspection and dynamic method creation
        for name in dir(self.memcache):
            # List our methods, ignoring private methods and ones that should be hidden to our CLI
            if not name.startswith('_') and not self._is_hidden(name):
                attr = getattr(self.memcache, name)
                if callable(attr):
                    # Make sure we only keep callable methods, let's pin the doc strings to them while we are at it
                    setattr(self.__class__, 'do_' + name, self._make_cmd(name))
                    doc = (getattr(attr, '__doc__') or '').strip()
                    if doc:  # Not everything has a docstring
                        setattr(self.__class__, 'help_' + name,
                                self._make_help(doc))

    @staticmethod
    def _make_cmd(name):
        # Whats a little functional passing between friends?
        # This is our core factory for creating dynamic methods
        def _get_stats(self, line):
            try:
                # Rabbit Holes can get pretty deep, better to keep a lid on it
                pp = pprint.PrettyPrinter(depth=4)
                pp.pprint(self.memcache.get_stats(line))
            except Exception, e:
                print_error(e)

        def handler(self, line):
            parts = line.split()
            try:
                # This is where the magic happens, get our function by name, pass the arguments, then pretty-print the results
                pprint.pprint(getattr(self.memcache, name)(*parts))
            except Exception, e:
                print_error(e)
Example #6
0
def stats(socket):
    client = Client([f'unix://{SOCKET_DIR}/{socket}'])
    stats = client.get_stats()
    if not stats:
        print(json.dumps({}))
    else:
        data = stats[0][1]
        data['hit_rate'] = "0"
        data['cache_rate'] = "0"

        # Calculate hit rate
        try:
            data['hit_rate'] = str(
                round(float(data['get_hits']) / float(data['cmd_get']) * 100))
            data['cache_rate'] = str(
                round(
                    float(data['bytes']) / float(data['limit_maxbytes']) *
                    100))
        except ZeroDivisionError:
            pass

        print(json.dumps(data))
Example #7
0
class MemcacheCli(cmd.Cmd, object):

  def __init__(self, host_list):
    super(MemcacheCli, self).__init__()
    self.memcache = Client(host_list) # Use our memcache library to initiate a connection
    self._check_connection(host_list) # Make sure we actually have connections, since our library doesn't make this apparent
    self.prompt = '(memcache) '
    # Time for some hardcore introspection and dynamic method creation
    for name in dir(self.memcache):
      # List our methods, ignoring private methods and ones that should be hidden to our CLI
      if not name.startswith('_') and not self._is_hidden(name):
        attr = getattr(self.memcache, name)
        if callable(attr):
          # Make sure we only keep callable methods, let's pin the doc strings to them while we are at it
          setattr(self.__class__, 'do_' + name, self._make_cmd(name))
          doc = (getattr(attr, '__doc__') or '').strip()
          if doc:  # Not everything has a docstring
            setattr(self.__class__, 'help_' + name, self._make_help(doc))

  @staticmethod
  def _make_cmd(name):
    # Whats a little functional passing between friends?
    # This is our core factory for creating dynamic methods
    def _get_stats(self, line):
      try:
        pp = pprint.PrettyPrinter(depth=4)
        pp.pprint(self.memcache.get_stats(line))
      except Exception, e:
        print_error(e)

    def handler(self, line):
        parts = line.split()
        try:
          # This is where the magic happens, get our function by name, pass the arguments, then pretty-print the results
          pprint.pprint(getattr(self.memcache, name)(*parts))
        except Exception, e:
          print_error(e)