Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
D
Django-Redis-Cache
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shared
Django-Redis-Cache
Commits
992d4e2c
Commit
992d4e2c
authored
May 17, 2010
by
Jannis Leidel
Committed by
Sean Bleier
May 29, 2010
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Re-introduced CacheClass.prepare_key to be able to override key generation in subclasses.
parent
72a899e7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
9 deletions
+17
-9
cache.py
redis_cache/cache.py
+17
-9
No files found.
redis_cache/cache.py
View file @
992d4e2c
...
...
@@ -38,15 +38,22 @@ class CacheClass(BaseCache):
port
=
6379
self
.
_cache
=
redis
.
Redis
(
host
=
host
,
port
=
port
,
db
=
db
,
password
=
password
)
def
prepare_key
(
self
,
key
):
"""
Hashes the key if length is greater than 250.
"""
return
smart_str
(
key
)
def
add
(
self
,
key
,
value
,
timeout
=
None
):
"""
Add a value to the cache, failing if the key already exists.
Returns ``True`` if the object was added, ``False`` if not.
"""
if
self
.
_cache
.
exists
(
smart_str
(
key
)):
key
=
self
.
prepare_key
(
key
)
if
self
.
_cache
.
exists
(
key
):
return
False
return
self
.
set
(
smart_str
(
key
)
,
value
,
timeout
)
return
self
.
set
(
key
,
value
,
timeout
)
def
get
(
self
,
key
,
default
=
None
):
"""
...
...
@@ -55,7 +62,7 @@ class CacheClass(BaseCache):
Returns unpicked value if key is found, ``None`` if not.
"""
# get the value from the cache
value
=
self
.
_cache
.
get
(
s
mart_str
(
key
))
value
=
self
.
_cache
.
get
(
s
elf
.
prepare_key
(
key
))
if
value
is
None
:
return
default
# pickle doesn't want a unicode!
...
...
@@ -67,11 +74,12 @@ class CacheClass(BaseCache):
"""
Persist a value to the cache, and set an optional expiration time.
"""
key
=
self
.
prepare_key
(
key
)
# pickle the value
value
=
base64
.
encodestring
(
pickle
.
dumps
(
value
,
pickle
.
HIGHEST_PROTOCOL
))
.
strip
()
# store the key/value pair
result
=
self
.
_cache
.
set
(
smart_str
(
key
)
,
value
)
result
=
self
.
_cache
.
set
(
key
,
value
)
# set expiration if needed
self
.
expire
(
key
,
timeout
)
# result is a boolean
...
...
@@ -82,19 +90,19 @@ class CacheClass(BaseCache):
Set content expiration, if necessary
"""
timeout
=
timeout
or
self
.
default_timeout
self
.
_cache
.
expire
(
s
mart_str
(
key
),
timeout
)
self
.
_cache
.
expire
(
s
elf
.
prepare_key
(
key
),
timeout
)
def
delete
(
self
,
key
):
"""
Remove a key from the cache.
"""
self
.
_cache
.
delete
(
s
mart_str
(
key
))
self
.
_cache
.
delete
(
s
elf
.
prepare_key
(
key
))
def
delete_many
(
self
,
keys
):
"""
Remove multiple keys at once.
"""
self
.
_cache
.
delete
(
*
map
(
s
mart_str
,
keys
))
self
.
_cache
.
delete
(
*
map
(
s
elf
.
prepare_key
,
keys
))
def
clear
(
self
):
"""
...
...
@@ -107,7 +115,7 @@ class CacheClass(BaseCache):
Retrieve many keys.
"""
recovered_data
=
SortedDict
()
results
=
self
.
_cache
.
mget
(
map
(
lambda
k
:
s
mart_str
(
k
),
keys
))
results
=
self
.
_cache
.
mget
(
map
(
lambda
k
:
s
elf
.
prepare_key
(
k
),
keys
))
for
key
,
value
in
zip
(
keys
,
results
):
if
value
is
None
:
continue
...
...
@@ -130,7 +138,7 @@ class CacheClass(BaseCache):
"""
safe_data
=
{}
for
key
,
value
in
data
.
iteritems
():
safe_data
[
s
mart_str
(
key
)]
=
base64
.
encodestring
(
safe_data
[
s
elf
.
prepare_key
(
key
)]
=
base64
.
encodestring
(
pickle
.
dumps
(
value
,
pickle
.
HIGHEST_PROTOCOL
))
.
strip
()
self
.
_cache
.
mset
(
safe_data
)
map
(
self
.
expire
,
safe_data
,
[
timeout
]
*
len
(
safe_data
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment