Module OSCCache
[hide private]
[frames] | no frames]

Source Code for Module OSCCache

  1  #!/usr/bin/env python 
  2   
  3  """Caching API for Storage Connect plug-ins. 
  4   
  5  Some of the operations provided by storage servers are very expensive. 
  6  They can take seconds or more to complete.  Since they often need to be 
  7  repeated, it makes sense to cache responses for a limited period of 
  8  time. 
  9   
 10  Any object may serve as a cache as long as it provides the following API:: 
 11   
 12      set(ss_uuid, key, value, expire_seconds=30) 
 13        - Stores a value for expire_seconds 
 14      get(ss_uuid, key) 
 15        - Returns the value for key, or None if key has expired or was 
 16          never stored 
 17      extend(ss_uuid, key, extend_seconds) 
 18        - Extends the expire time for key; does nothing if key has already 
 19          expired 
 20      clear(ss_uuid) 
 21        - Drops all keys for ss_uuid 
 22   
 23  """ 
 24   
 25   
 26  """ 
 27  @copyright: Copyright (C) 2010 Oracle and/or its affiliates. All rights reserved. 
 28   
 29  @license:   See the file COPYING for redistribution information. 
 30   
 31  @summary:   Caching API for Storage Connect plug-ins. 
 32  """ 
 33   
 34   
 35  import time 
 36   
 37   
 38  __all__ = ['OSCDefaultCache'] 
 39   
 40   
41 -class OSCDefaultCache(object):
42 43 """A basic in-memory cache for simple users 44 45 This simple cache provides the basic caching API for via an 46 in-memory store. No data is persisted, thus it is not available 47 to other processes. 48 49 """ 50
51 - def __init__(self):
52 self._data = {} 53 self._expire = {}
54
55 - def _check_expire(self, ss_uuid, key):
56 ss_cache = self._data.get(ss_uuid, {}) 57 ss_expire = self._expire.get(ss_uuid, {}) 58 if ss_cache.has_key(key) and (time.time() > ss_expire[key]): 59 del ss_cache[key] 60 del ss_expire[key]
61
62 - def get(self, ss_uuid, key):
63 """Retrieve a cached value""" 64 65 self._check_expire(ss_uuid, key) 66 ss_cache = self._data.get(ss_uuid, {}) 67 return ss_cache.get(key, None)
68
69 - def set(self, ss_uuid, key, value, expire_seconds=30):
70 """Store a cached value for the specified server""" 71 72 if not self._data.has_key(ss_uuid): 73 self._data[ss_uuid] = {} 74 self._data[ss_uuid][key] = value 75 if not self._expire.has_key(ss_uuid): 76 self._expire[ss_uuid] = {} 77 self._expire[ss_uuid][key] = time.time() + expire_seconds
78
79 - def extend(self, ss_uuid, key, extend_seconds):
80 """Extend the expire timeout for a cached value""" 81 82 self._check_expire(ss_uuid, key) 83 if self._expire.get(ss_uuid, {}).has_key(key): 84 self._expire[ss_uuid][key] += extend_seconds
85
86 - def clear(self, ss_uuid):
87 """Clear the cache for the specified server""" 88 89 if self._data.has_key(ss_uuid): 90 del self._data[ss_uuid] 91 if self._expire.has_key(ss_uuid): 92 del self._expire[ss_uuid]
93
94 - def read(self, ss_uuid):
95 """read the entire cache for the specified server""" 96 97 if not self._data.has_key(ss_uuid): 98 return None 99 return self._data[ss_uuid]
100
101 - def write(self, ss_uuid, data):
102 """write the entire cache for the specified server""" 103 104 if self._data.has_key(ss_uuid): 105 self.clear(ss_uuid) 106 self._data[ss_uuid] = data
107