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
127a5eaa
Commit
127a5eaa
authored
Jul 21, 2015
by
Sean Bleier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update function signatures to use DEFAULT_TIMEOUT.
parent
409feece
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
14 deletions
+20
-14
api.rst
docs/api.rst
+3
-3
base.py
redis_cache/backends/base.py
+17
-11
No files found.
docs/api.rst
View file @
127a5eaa
...
@@ -13,14 +13,14 @@ Standard Django Cache API
...
@@ -13,14 +13,14 @@ Standard Django Cache API
:rtype: Value that was cached.
:rtype: Value that was cached.
.. function:: add(self, key, value[, timeout=
None
]):
.. function:: add(self, key, value[, timeout=
DEFAULT_TIMEOUT
]):
Add a value to the cache, failing if the key already exists.
Add a value to the cache, failing if the key already exists.
:param key: Location of the value
:param key: Location of the value
:param value: Value to cache
:param value: Value to cache
:param timeout: Number of seconds to hold value in cache.
:param timeout: Number of seconds to hold value in cache.
:type timeout: Number of seconds or
None
:type timeout: Number of seconds or
DEFAULT_TIMEOUT
:rtype: True if object was added and False if it already exists.
:rtype: True if object was added and False if it already exists.
...
@@ -33,7 +33,7 @@ Standard Django Cache API
...
@@ -33,7 +33,7 @@ Standard Django Cache API
:param key: Location of the value
:param key: Location of the value
:param value: Value to cache
:param value: Value to cache
:param timeout: Number of seconds to hold value in cache.
:param timeout: Number of seconds to hold value in cache.
:type timeout: Number of seconds or
None
:type timeout: Number of seconds or
DEFAULT_TIMEOUT
.. function:: delete(self, key):
.. function:: delete(self, key):
...
...
redis_cache/backends/base.py
View file @
127a5eaa
from
django.core.cache.backends.base
import
BaseCache
,
InvalidCacheBackendError
from
django.core.cache.backends.base
import
BaseCache
,
InvalidCacheBackendError
from
django.core.exceptions
import
ImproperlyConfigured
from
django.core.exceptions
import
ImproperlyConfigured
from
django.utils
import
importlib
from
django.utils.importlib
import
import_module
try
:
try
:
import
redis
import
redis
...
@@ -12,7 +10,7 @@ except ImportError:
...
@@ -12,7 +10,7 @@ except ImportError:
from
redis.connection
import
DefaultParser
from
redis.connection
import
DefaultParser
from
redis_cache.compat
import
smart_bytes
,
DEFAULT_TIMEOUT
from
redis_cache.compat
import
DEFAULT_TIMEOUT
from
redis_cache.connection
import
pool
from
redis_cache.connection
import
pool
from
redis_cache.utils
import
(
from
redis_cache.utils
import
(
CacheKey
,
get_servers
,
parse_connection_kwargs
,
import_class
CacheKey
,
get_servers
,
parse_connection_kwargs
,
import_class
...
@@ -216,16 +214,26 @@ class BaseRedisCache(BaseCache):
...
@@ -216,16 +214,26 @@ class BaseRedisCache(BaseCache):
def
make_keys
(
self
,
keys
,
version
=
None
):
def
make_keys
(
self
,
keys
,
version
=
None
):
return
[
self
.
make_key
(
key
,
version
=
version
)
for
key
in
keys
]
return
[
self
.
make_key
(
key
,
version
=
version
)
for
key
in
keys
]
def
get_timeout
(
self
,
timeout
):
if
timeout
is
DEFAULT_TIMEOUT
:
timeout
=
self
.
default_timeout
if
timeout
is
not
None
:
timeout
=
int
(
timeout
)
return
timeout
####################
####################
# Django cache api #
# Django cache api #
####################
####################
@
get_client
(
write
=
True
)
@
get_client
(
write
=
True
)
def
add
(
self
,
client
,
key
,
value
,
timeout
=
None
):
def
add
(
self
,
client
,
key
,
value
,
timeout
=
DEFAULT_TIMEOUT
):
"""Add a value to the cache, failing if the key already exists.
"""Add a value to the cache, failing if the key already exists.
Returns ``True`` if the object was added, ``False`` if not.
Returns ``True`` if the object was added, ``False`` if not.
"""
"""
timeout
=
self
.
get_timeout
(
timeout
)
return
self
.
_set
(
client
,
key
,
self
.
prep_value
(
value
),
timeout
,
_add_only
=
True
)
return
self
.
_set
(
client
,
key
,
self
.
prep_value
(
value
),
timeout
,
_add_only
=
True
)
@
get_client
()
@
get_client
()
...
@@ -259,11 +267,7 @@ class BaseRedisCache(BaseCache):
...
@@ -259,11 +267,7 @@ class BaseRedisCache(BaseCache):
def
set
(
self
,
client
,
key
,
value
,
timeout
=
DEFAULT_TIMEOUT
):
def
set
(
self
,
client
,
key
,
value
,
timeout
=
DEFAULT_TIMEOUT
):
"""Persist a value to the cache, and set an optional expiration time.
"""Persist a value to the cache, and set an optional expiration time.
"""
"""
if
timeout
is
DEFAULT_TIMEOUT
:
timeout
=
self
.
get_timeout
(
timeout
)
timeout
=
self
.
default_timeout
if
timeout
is
not
None
:
timeout
=
int
(
timeout
)
result
=
self
.
_set
(
client
,
key
,
self
.
prep_value
(
value
),
timeout
,
_add_only
=
False
)
result
=
self
.
_set
(
client
,
key
,
self
.
prep_value
(
value
),
timeout
,
_add_only
=
False
)
...
@@ -314,7 +318,7 @@ class BaseRedisCache(BaseCache):
...
@@ -314,7 +318,7 @@ class BaseRedisCache(BaseCache):
def
_set_many
(
self
,
client
,
data
):
def
_set_many
(
self
,
client
,
data
):
return
client
.
mset
(
data
)
return
client
.
mset
(
data
)
def
set_many
(
self
,
data
,
timeout
=
None
,
version
=
None
):
def
set_many
(
self
,
data
,
timeout
=
DEFAULT_TIMEOUT
,
version
=
None
):
"""Set a bunch of values in the cache at once from a dict of key/value
"""Set a bunch of values in the cache at once from a dict of key/value
pairs. This is much more efficient than calling set() multiple times.
pairs. This is much more efficient than calling set() multiple times.
...
@@ -381,10 +385,12 @@ class BaseRedisCache(BaseCache):
...
@@ -381,10 +385,12 @@ class BaseRedisCache(BaseCache):
raise
NotImplementedError
raise
NotImplementedError
@
get_client
(
write
=
True
)
@
get_client
(
write
=
True
)
def
get_or_set
(
self
,
client
,
key
,
func
,
timeout
=
None
):
def
get_or_set
(
self
,
client
,
key
,
func
,
timeout
=
DEFAULT_TIMEOUT
):
if
not
callable
(
func
):
if
not
callable
(
func
):
raise
Exception
(
"Must pass in a callable"
)
raise
Exception
(
"Must pass in a callable"
)
timeout
=
self
.
get_timeout
(
timeout
)
dogpile_lock_key
=
"_lock"
+
key
.
_versioned_key
dogpile_lock_key
=
"_lock"
+
key
.
_versioned_key
dogpile_lock
=
client
.
get
(
dogpile_lock_key
)
dogpile_lock
=
client
.
get
(
dogpile_lock_key
)
...
...
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