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
5847aefc
Commit
5847aefc
authored
Jul 21, 2015
by
Sean Bleier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add initial docs for the api and advanced configuration.
parent
4c7b9d58
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
440 additions
and
0 deletions
+440
-0
advanced_configuration.rst
docs/advanced_configuration.rst
+284
-0
api.rst
docs/api.rst
+156
-0
No files found.
docs/advanced_configuration.rst
View file @
5847aefc
Advanced Configuration
**********************
Example Setting
---------------
.. code:: python
CACHES = {
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': '127.0.0.1:6379',
'OPTIONS': {
'DB': 1,
'PASSWORD': 'yadayada',
'PARSER_CLASS': 'redis.connection.HiredisParser',
'CONNECTION_POOL_CLASS': 'redis.BlockingConnectionPool',
'PICKLE_VERSION': -1,
},
},
}
Pluggable Backends
------------------
django-redis-cache comes with a couple pluggable backends, one for a unified
keyspace and one for a sharded keyspace. The former can be in the form of a
single redis server or several redis servers setup in a primary/secondary
configuration. The primary is used for writing and secondaries are
replicated versions of the primary for read-access.
.. code:: python
# Unified keyspace
CACHES = {
'default': {
'BACKEND': 'redis_cache.RedisCache',
...
}
}
# Sharded keyspace
CACHES = {
'default': {
'BACKEND': 'redis_cache.ShardedRedisCache',
...
}
}
Location Schemes
----------------
The ``LOCATION`` contains the information for the redis server's location,
which can be the address/port or the server path to the unix domain socket. The
location can be a single string or a list of strings. Several schemes for
defining the location can be used. Here is a list of example schemes:
* ``127.0.0.1:6379``
* ``/path/to/socket``
* ``redis://[:password]@localhost:6379/0``
* ``rediss://[:password]@localhost:6379/0``
* ``unix://[:password]@/path/to/socket.sock?db=0``
Database Number
---------------
The ``DB`` option will allow key/values to exist in a different keyspace. The
``DB`` value can either be defined in the ``OPTIONS`` or in the ``LOCATION``
scheme.
.. code:: python
CACHES = {
'default': {
'OPTIONS': {
'DB': 1,
..
},
...
}
}
Password
--------
If the redis server is password protected, you can specify the ``PASSWORD``
option.
.. code:: python
CACHES = {
'default': {
'OPTIONS': {
'PASSWORD': 'yadayada',
...
},
...
}
}
Pluggable Parse Classes
-----------------------
`redis-py`_ comes with two parsers: ``HiredisParser`` and ``PythonParser``.
The former uses the `hiredis`_ library to parse responses from the redis
server, while the latter uses Python. Hiredis is a library that uses C, so it
is much faster than the python parser, but requires installing the library
separately.
To install `hiredis`_:
``pip install hiredis``
.. code:: python
CACHES = {
'default': {
'OPTIONS': {
'PARSER_CLASS': 'redis.connection.HiredisParser',
...
},
...
}
}
Pickle Version
--------------
When using the pickle serializer, you can use ``PICKLE_VERSION`` to specify
the protocol version of pickle you want to use to serialize your python objects.
.. code:: python
CACHES = {
'default': {
'OPTIONS': {
'PICKLE_VERSION': 2,
...
},
...
},
}
Socket Timeout and Socket Create Timeout
----------------------------------------
When working with a TCP connection, it may be beneficial to set the
``SOCKET_TIMEOUT`` and ``SOCKET_CONNECT_TIMEOUT`` options to prevent your
app from blocking indefinitely.
If provided, the socket will time out when the established connection exceeds
``SOCKET_TIMEOUT`` seconds.
Similarly, the socket will time out if it takes more than
``SOCKET_CONNECT_TIMEOUT`` seconds to establish.
.. code:: python
CACHES={
'default': {
'OPTIONS': {
'SOCKET_TIMEOUT': 5,
'SOCKET_CONNECT_TIMEOUT': 5,
...
}
...
}
}
Connection Pool
---------------
There is an associated overhead when creating connections to a redis server.
Therefore, it's beneficial to create a pool of connections that the cache can
reuse to send or retrieve data from the redis server.
``CONNECTION_POOL_CLASS`` can be used to specify a class to use for the
connection pool. In addition, you can provide custom keyword arguments using
the ``CONNECTION_POOL_CLASS_KWARGS`` option that will be passed into the class
when it's initialized.
.. code:: python
CACHES = {
'default': {
'OPTIONS': {
'CONNECTION_POOL_CLASS': 'redis.BlockingConnectionPool',
'CONNECTION_POOL_CLASS_KWARGS': {
'max_connections': 50,
'timeout': 20,
...
},
...
},
...
}
}
Pluggable Serializers
---------------------
You can use ``SERIALIZER_CLASS`` to specify a class that will
serialize/deserialize data. In addition, you can provide custom keyword
arguments using the ``SERIALIZER_CLASS_KWARGS`` option that will be passed into
the class when it's initialized.
The default serializer in django-redis-cache is the pickle serializer. It can
serialize most python objects, but is slow and not always safe. Also included
are serializer using json, msgpack, and yaml. Not all serializers can handle
Python objects, so they are limited to primitive data types.
.. code:: python
CACHES = {
'default': {
'OPTIONS': {
'SERIALIZER_CLASS': 'redis_cache.serializers.PickleSerializer',
'SERIALIZER_CLASS_KWARGS': {
'pickle_version': -1
},
...
},
...
}
}
Pluggable Compressors
---------------------
You can use ``COMPRESSOR_CLASS`` to specify a class that will
compress/decompress data. Use the ``COMPRESSOR_CLASS_KWARGS`` option to
initialize the compressor class.
The default compressor is ``NoopCompressor`` which does not compress your data.
However, if you want to compress your data, you can use one of the included
compressor classes:
.. code:: python
# zlib compressor
CACHES = {
'default': {
'OPTIONS': {
'COMPRESSOR_CLASS': 'redis_cache.compressors.ZLibCompressor',
'COMPRESSOR_CLASS_KWARGS': {
'level': 5, # 0 - 9; 0 - no compression; 1 - fastest, biggest; 9 - slowest, smallest
},
...
},
...
}
}
# bzip2 compressor
CACHES = {
'default': {
'OPTIONS': {
'COMPRESSOR_CLASS': 'redis_cache.compressors.BZip2Compressor',
'COMPRESSOR_CLASS_KWARGS': {
'compresslevel': 5, # 1 - 9; 1 - fastest, biggest; 9 - slowest, smallest
},
...
},
...
}
}
.. _redis-py: http://github.com/andymccurdy/redis-py/
.. _hiredis: https://pypi.python.org/pypi/hiredis/
docs/api.rst
View file @
5847aefc
API Usage
*********
Standard Django Cache API
-------------------------
.. function:: get(self, key[, default=None]):
Retrieves a value from the cache.
:param key: Location of the value
:param default: Value to return if key does not exist in cache.
:rtype: Value what was cached.
.. function:: add(self, key, value[, timeout=None]):
Add a value to the cache, failing if the key already exists.
:param key: Location of the value
:param value: Value to cache
:param timeout: Number of seconds to hold value in cache.
:type timeout: Number of seconds or None
:rtype: True if object was added and False if it already exists.
.. function:: set(self, key, value, timeout=DEFAULT_TIMEOUT):
Sets a value to the cache, regardless of whether it exists.
If ``timeout == None``, then cache is set indefinitely. Otherwise, timeout defaults to the defined ``DEFAULT_TIMEOUT``.
:param key: Location of the value
:param value: Value to cache
:param timeout: Number of seconds to hold value in cache.
:type timeout: Number of seconds or None
.. function:: delete(self, key):
Removes a key from the cache
:param key: Location of the value
.. function:: delete_many(self, keys[, version=None]):
Removes multiple keys at once.
:param key: Location of the value
:param version: Version of keys
.. function:: clear(self[, version=None]):
Flushes the cache. If version is provided, all keys under the version number will be deleted. Otherwise, all keys will be flushed.
:param version: Version of keys
.. function:: get_many(self, keys[, version=None]):
Retrieves many keys at once.
:param keys: an iterable of keys to retrieve.
:rtype: Dict of keys mapping to their values.
.. function:: set_many(self, data[, timeout=None, version=None]):
Set many values in the cache at once from a dict of key/value pairs. This is much more efficient than calling set() multiple times and is atomic.
:param data: dict of key/value pairs to cache.
:param timeout: Number of seconds to hold value in cache.
:type timeout: Number of seconds or None
.. function:: incr(self, key[, delta=1]):
Add delta to value in the cache. If the key does not exist, raise a `ValueError` exception.
:param key: Location of the value
:param delta: Integer used to increment a value.
:type delta: Integer
.. function:: incr_version(self, key[, delta=1, version=None]):
Adds delta to the cache version for the supplied key. Returns the new version.
:param key: Location of the value
:param delta: Integer used to increment a value.
:type delta: Integer
:param version: Version of key
:type version: Integer or None
Cache Methods Provided by django-redis-cache
--------------------------------------------
.. function:: has_key(self, key):
Returns True if the key is in the cache and has not expired.
:param key: Location of the value
:rtype: bool
.. function:: ttl(self, key):
Returns the 'time-to-live' of a key. If the key is not volatile, i.e. it has not set an expiration, then the value returned is None.
Otherwise, the value is the number of seconds remaining. If the key does not exist, 0 is returned.
:param key: Location of the value
:rtype: Integer or None
.. function:: delete_pattern(pattern[, version=None]):
Deletes keys matching the glob-style pattern provided.
:param pattern: Glob-style pattern used to select keys to delete.
:param version: Version of the keys
.. function:: get_or_set(self, key, func[, timeout=None]):
Retrieves a key value from the cache and sets the value if it does not exist.
:param key: Location of the value
:param func: Callable used to set the value if key does not exist.
:param timeout: Number of seconds to hold value in cache.
:type timeout: Number of seconds or None
.. function:: reinsert_keys(self):
Helper function to reinsert keys using a different pickle protocol version.
.. function:: persist(self, key):
Removes the timeout on a key.
Equivalent to setting a timeout of None in a set command.
:param key: Location of the value
:rtype: bool
.. function:: expire(self, key, timeout):
Set the expire time on a key
:param key: Location of the value
:rtype: bool
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