Skip to content

Commit 73b01b3

Browse files
author
shuwen5
committed
cacheable support msgpack.pack cached item
1 parent 4d5bfd2 commit 73b01b3

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

cacheable/README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Least Recently Used Cache.
103103
## cacheable.Cacheable
104104

105105
**syntax**:
106-
`cacheable.Cacheable(capacity=1024 * 4, timeout=60, is_deepcopy=True, mutex_update=False)`
106+
`cacheable.Cacheable(capacity=1024 * 4, timeout=60, is_deepcopy=True, is_pack=False, mutex_update=False)`
107107

108108
Create a `LRU` object, all items will be cached in it.
109109

@@ -120,6 +120,13 @@ Create a `LRU` object, all items will be cached in it.
120120

121121
- `False`: return reference of cached item
122122

123+
- `is_pack`: `cacheable.cache` return a decorator that use `is_pack`
124+
to return `msgpack.pack` item.
125+
126+
- `True`: return `msgpack.pack` of cached item
127+
128+
- `False`: return reference of cached item
129+
123130
- `mutex_update`: allows only one thread to update the cache item.
124131
Default is `False`.
125132

@@ -187,7 +194,7 @@ so can get value and set value like `dict`
187194
## cacheable.cache
188195

189196
**syntax**:
190-
`cacheable.cache(name, capacity=1024 * 4, timeout=60, is_deepcopy=True, mutex_update=False)`
197+
`cacheable.cache(name, capacity=1024 * 4, timeout=60, is_deepcopy=True, is_pack=False, mutex_update=False)`
191198

192199
If not exist, create a `cacheable.Cacheable` and save it, else use exist one.
193200

cacheable/cacheable.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import threading
77
import time
8+
import msgpack
89

910
logger = logging.getLogger(__name__)
1011
_lock_mutex_update = threading.RLock()
@@ -95,10 +96,11 @@ def _cleanup(self):
9596
class Cacheable(object):
9697

9798
def __init__(self, capacity=1024 * 4, timeout=60,
98-
is_deepcopy=True, mutex_update=False):
99+
is_deepcopy=True, is_pack=False, mutex_update=False):
99100

100101
self.lru = LRU(capacity, timeout)
101102
self.is_deepcopy = is_deepcopy
103+
self.is_pack = is_pack
102104
self.mutex_update = mutex_update
103105

104106
def _arg_str(self, args, argkv):
@@ -124,11 +126,18 @@ def func_wrapper(*args, **argkv):
124126
val = self.lru[generate_key]
125127
except KeyError:
126128
val = fun(*args, **argkv)
129+
if self.is_pack:
130+
val = msgpack.packb(val, use_bin_type=True)
127131
self.lru[generate_key] = val
128132
else:
129133
val = fun(*args, **argkv)
134+
if self.is_pack:
135+
val = msgpack.packb(val, use_bin_type=True)
130136
self.lru[generate_key] = val
131137

138+
if self.is_pack:
139+
val = msgpack.unpackb(val, raw=False)
140+
132141
if self.is_deepcopy:
133142
return copy.deepcopy(val)
134143
else:
@@ -140,7 +149,8 @@ def func_wrapper(*args, **argkv):
140149
caches = {}
141150

142151

143-
def cache(name, capacity=1024 * 4, timeout=60, is_deepcopy=True, mutex_update=False):
152+
def cache(name, capacity=1024 * 4, timeout=60,
153+
is_deepcopy=True, is_pack=False, mutex_update=False):
144154

145155
c = caches.get(name)
146156
if c is None:

requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ objgraph==3.1.0
6262

6363
# geventwebsocket
6464
gevent-websocket==0.10.1
65+
66+
# msgpack
67+
msgpack==0.6.1

0 commit comments

Comments
 (0)