thirdparty/google_appengine/google/appengine/api/memcache/memcache_stub.py
changeset 686 df109be0567c
parent 149 f2e327a7c5de
child 1278 a7766286a7be
equal deleted inserted replaced
685:a440ced9a75f 686:df109be0567c
    20 
    20 
    21 
    21 
    22 import logging
    22 import logging
    23 import time
    23 import time
    24 
    24 
       
    25 from google.appengine.api import apiproxy_stub
    25 from google.appengine.api import memcache
    26 from google.appengine.api import memcache
    26 from google.appengine.api.memcache import memcache_service_pb
    27 from google.appengine.api.memcache import memcache_service_pb
    27 
    28 
    28 MemcacheSetResponse = memcache_service_pb.MemcacheSetResponse
    29 MemcacheSetResponse = memcache_service_pb.MemcacheSetResponse
    29 MemcacheSetRequest = memcache_service_pb.MemcacheSetRequest
    30 MemcacheSetRequest = memcache_service_pb.MemcacheSetRequest
    89   def CheckLocked(self):
    90   def CheckLocked(self):
    90     """Returns True if this entry was deleted but has not yet timed out."""
    91     """Returns True if this entry was deleted but has not yet timed out."""
    91     return self.locked and not self.CheckExpired()
    92     return self.locked and not self.CheckExpired()
    92 
    93 
    93 
    94 
    94 class MemcacheServiceStub(object):
    95 class MemcacheServiceStub(apiproxy_stub.APIProxyStub):
    95   """Python only memcache service stub.
    96   """Python only memcache service stub.
    96 
    97 
    97   This stub keeps all data in the local process' memory, not in any
    98   This stub keeps all data in the local process' memory, not in any
    98   external servers.
    99   external servers.
    99   """
   100   """
   100 
   101 
   101   def __init__(self, gettime=time.time):
   102   def __init__(self, gettime=time.time, service_name='memcache'):
   102     """Initializer.
   103     """Initializer.
   103 
   104 
   104     Args:
   105     Args:
   105       gettime: time.time()-like function used for testing.
   106       gettime: time.time()-like function used for testing.
   106     """
   107       service_name: Service name expected for all calls.
       
   108     """
       
   109     super(MemcacheServiceStub, self).__init__(service_name)
   107     self._gettime = gettime
   110     self._gettime = gettime
   108     self._ResetStats()
   111     self._ResetStats()
   109 
   112 
   110     self._the_cache = {}
   113     self._the_cache = {}
   111 
   114 
   113     """Resets statistics information."""
   116     """Resets statistics information."""
   114     self._hits = 0
   117     self._hits = 0
   115     self._misses = 0
   118     self._misses = 0
   116     self._byte_hits = 0
   119     self._byte_hits = 0
   117     self._cache_creation_time = self._gettime()
   120     self._cache_creation_time = self._gettime()
   118 
       
   119   def MakeSyncCall(self, service, call, request, response):
       
   120     """The main RPC entry point.
       
   121 
       
   122     Args:
       
   123       service: Must be name as defined by sub class variable SERVICE.
       
   124       call: A string representing the rpc to make.  Must be part of
       
   125         MemcacheService.
       
   126       request: A protocol buffer of the type corresponding to 'call'.
       
   127       response: A protocol buffer of the type corresponding to 'call'.
       
   128     """
       
   129     assert service == 'memcache'
       
   130     assert request.IsInitialized()
       
   131 
       
   132     attr = getattr(self, '_Dynamic_' + call)
       
   133     attr(request, response)
       
   134 
   121 
   135   def _GetKey(self, key):
   122   def _GetKey(self, key):
   136     """Retrieves a CacheEntry from the cache if it hasn't expired.
   123     """Retrieves a CacheEntry from the cache if it hasn't expired.
   137 
   124 
   138     Does not take deletion timeout into account.
   125     Does not take deletion timeout into account.