Memcache
Memcache is a caching system that works by temporarily storing information in memory that would usually be retrieved from a database.
Memcached is an open source, high-performance, distributed memory object caching system.
Cache
Client Cache(Browser Example)
Expires -- The datetime after which the response will be considered as 'out of date'
- Last-Modified -- datetime indication after which the response will be considered 'out of date' (If-Modified-Since)
- Cache-Control -- a directives about whether the resource must be cached and its maximal age of validity.
- Date -- origin servers datetime
(Static Resource can be set in the web server configuration, and for dynamic one [pull from database], these headers must be provided and checked from the applications itself.)
- Server Cache
A single resource is requested by multiple of clients.(caching proxy), caching all of the page.
- Application cache
Store part of the data in cache, and retrieve it later, by using Memcached.
Each cached item is represented in the cache with unique key.
Using Memcached with Django
Memcached is a tool that allows you to store key-value pairs in your memory The keys are limited to 250 Bytes and for better performance the value size is limited to 1 MB.
- Installation and Setup
apt-get install memcached
apt-get install python-memcache
# Run another instance
# -p is for TCP port number
# -U is for UDP port number
# -u is for user name
# -d runs memcached as daemon process
$ memcached -p 11111 -U 11111 -u user
You would run multiple instances of Memcached server through a single installation.
- Django Setting
CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
- Use memcache in django
from django.core.cache import cache
...
did = 'unique_string'
result = cache.get('result')
if result:
result[did] = pm_value
cache.set('result', result, None)
else:
result = {}
result[did] = pm_value
cache.add('result', result, None)
return Response(status=status.HTTP_200_OK)
- Using telnet to test
Format: set key flags exptime bytes
$telnet 127.0.0.1 11211
set variable 0 900 9
memcached
STORED
get variable
- key: It it the name of the unique key by which data is accessed
- flags: It is the 32-bit unsigned integer that the server stores with the data provided by the user, and return along with the data when the item is retrieved.
- exptime: It is the expiration time(seconds) of data stored in cache. A 0 value means "never expire".
- bytes: This is the length of the data in bytes that needs to be stored in memcached.
- noreply(optional) : This parameter informs the server not to send any reply.
- value: It is the data that needs to be stored.
output:
STORED: Indicate success
ERROR: Indicate incorrect syntax or error while saving data
Commands
Set(The set command is used to set a value to key, if the key does not exist, a new key is created and value is assigned to that key)
- Add(Set a value to a new key, if the key already exists, then it gives the output NOT_STORED)
- Replace(Used to replace the value of an existing key, if the key does not exists, then it gives the output NOT_STORED)
- Append (USed to add data in an existing key, this data is added at the end of the previous value)