Python 26 Django cache

A cache

1, the configuration

= CACHES {
     ' default ' = {
         ' BACKEND ' : ' django.core.cache.backend.locmem.LocMemCache ' ,   # which is stored in memory 
    ' LOCATION ' : ' UNIQUE-Snowflake ' ,   # which is the unique identifier 
    ' TIMEOUT ' : 300,   # timeout, default 300, None never expire, 0 expired immediately 
    ' the OPTIONS ' : {
         ' MAX_ENTRIES ' : 300,   # maximum number of cache, default 300 
        'CULL_FREQUENCY' : 3,   # cache after reaching the maximum number, excluding the proportion of one third 
        } 
    }           
}
= CACHES {
     ' default ' = {
         ' BACKEND ' : ' django.core.cache.backend.filebased.FileBasedCache ' ,   # which is saved to a file 
    ' LOCATION ' : ' E: XXX // ' ,   # which is stored path 
    ' tIMEOUT ' : 300,   # timeout, default 300, never expire None, 0 represents expire immediately 
    ' the OPTIONS ' : {
         ' MAX_ENTRIES ' : 300,   # maximum number of cache, default 300 
        'CULL_FREQUENCY' : 3,   # cache after reaching the maximum number, excluding the proportion of one third 
        } 
    }           
}
In the file
= CACHES {
     ' default ' = {
         ' BACKEND ' : ' django.core.cache.backend.dummy.DummyCache ' ,   # shown here without any cache, just a placeholder to facilitate development and debugging 
    ' LOCATION ' : ' UNIQUE-Snowflake ' ,   # this is a unique identifier 
    ' tIMEOUT ' : 300,   # timeout, default 300, None never expire, 0 expired immediately 
    ' the OPTIONS ' : {
         ' MAX_ENTRIES ' : 300,   # maximum number of cache, default 300 
        'CULL_FREQUENCY ' :. 3,   # cache after reaching the maximum number, excluding the proportion of one third 
        } 
    }           
}
Development and debugging

2, application

① single cache

from django.views.decorators.cache Import cache_page 

@cache_page ( 15 ) # access again within fifteen seconds will not print 111, even if the database update will not refresh the page data within the time
 DEF user_list (Request):
     Print ( " 111 " )
     return the render (Request, ' xx.html ' )

② the station's cache

# Get the last acquired cache needs request_process, since the front middleware may have some data or check operations, such csrf. Therefore, we need to get on the final cache middleware middleware configuration 
# storage cache needs to be stored in the last process_response, because the front of the middleware might be some data processing. It is necessary to store the buffer at the beginning of the middleware middleware configuration 

MIDDLEWARE = {
     ' django.middleware.cache.UpdateCacheMiddleware ' ,
     # other middleware 
    ' django.middleware.cache.FetchCacheMiddleware ' ,   
} 

CACHE_MIDDLEWARE_SECONDS = 300 # timeout, default 300

③ local cache

% Load Cache%} { 

{% Cache 100 'XX'}% passed a timeout, a key name 
    to put some data 
{% endcache%}

 

Second, serialization

It is to serialize the data into recognizable, string format transmittable

The default time can not json serialized objects, but can be customized to implement class object serialization time.

import json

class JsonCustomerEncoder(json.Encoder):
    def default(self, field):
        if isinstance(field, datetime):
            return field.strftime("%Y-%m-%h %H-%M-%S")
        elif isinstance(field, date):
            return dield.strftime("%Y-%m-%h")
        else:
            return json.JsonEncoder.default(self, field)

json.dumps(date, cls=JsonCustomerEncoder)

 

Guess you like

Origin www.cnblogs.com/yinwenjie/p/11272288.html