Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Memory Backends 

3--------------- 

4 

5Provides simple dictionary-based backends. 

6 

7The two backends are :class:`.MemoryBackend` and :class:`.MemoryPickleBackend`; 

8the latter applies a serialization step to cached values while the former 

9places the value as given into the dictionary. 

10 

11""" 

12 

13from ..api import CacheBackend 

14from ..api import NO_VALUE 

15from ...util.compat import pickle 

16 

17 

18class MemoryBackend(CacheBackend): 

19 """A backend that uses a plain dictionary. 

20 

21 There is no size management, and values which 

22 are placed into the dictionary will remain 

23 until explicitly removed. Note that 

24 Dogpile's expiration of items is based on 

25 timestamps and does not remove them from 

26 the cache. 

27 

28 E.g.:: 

29 

30 from dogpile.cache import make_region 

31 

32 region = make_region().configure( 

33 'dogpile.cache.memory' 

34 ) 

35 

36 

37 To use a Python dictionary of your choosing, 

38 it can be passed in with the ``cache_dict`` 

39 argument:: 

40 

41 my_dictionary = {} 

42 region = make_region().configure( 

43 'dogpile.cache.memory', 

44 arguments={ 

45 "cache_dict":my_dictionary 

46 } 

47 ) 

48 

49 

50 """ 

51 

52 pickle_values = False 

53 

54 def __init__(self, arguments): 

55 self._cache = arguments.pop("cache_dict", {}) 

56 

57 def get(self, key): 

58 value = self._cache.get(key, NO_VALUE) 

59 if value is not NO_VALUE and self.pickle_values: 

60 value = pickle.loads(value) 

61 return value 

62 

63 def get_multi(self, keys): 

64 ret = [self._cache.get(key, NO_VALUE) for key in keys] 

65 if self.pickle_values: 

66 ret = [ 

67 pickle.loads(value) if value is not NO_VALUE else value 

68 for value in ret 

69 ] 

70 return ret 

71 

72 def set(self, key, value): 

73 if self.pickle_values: 

74 value = pickle.dumps(value, pickle.HIGHEST_PROTOCOL) 

75 self._cache[key] = value 

76 

77 def set_multi(self, mapping): 

78 pickle_values = self.pickle_values 

79 for key, value in mapping.items(): 

80 if pickle_values: 

81 value = pickle.dumps(value, pickle.HIGHEST_PROTOCOL) 

82 self._cache[key] = value 

83 

84 def delete(self, key): 

85 self._cache.pop(key, None) 

86 

87 def delete_multi(self, keys): 

88 for key in keys: 

89 self._cache.pop(key, None) 

90 

91 

92class MemoryPickleBackend(MemoryBackend): 

93 """A backend that uses a plain dictionary, but serializes objects on 

94 :meth:`.MemoryBackend.set` and deserializes :meth:`.MemoryBackend.get`. 

95 

96 E.g.:: 

97 

98 from dogpile.cache import make_region 

99 

100 region = make_region().configure( 

101 'dogpile.cache.memory_pickle' 

102 ) 

103 

104 The usage of pickle to serialize cached values allows an object 

105 as placed in the cache to be a copy of the original given object, so 

106 that any subsequent changes to the given object aren't reflected 

107 in the cached value, thus making the backend behave the same way 

108 as other backends which make use of serialization. 

109 

110 The serialization is performed via pickle, and incurs the same 

111 performance hit in doing so as that of other backends; in this way 

112 the :class:`.MemoryPickleBackend` performance is somewhere in between 

113 that of the pure :class:`.MemoryBackend` and the remote server oriented 

114 backends such as that of Memcached or Redis. 

115 

116 Pickle behavior here is the same as that of the Redis backend, using 

117 either ``cPickle`` or ``pickle`` and specifying ``HIGHEST_PROTOCOL`` 

118 upon serialize. 

119 

120 .. versionadded:: 0.5.3 

121 

122 """ 

123 

124 pickle_values = True