Author: |
Dreampuf |
Date: |
23:22:50 |
Directory
- 1 Overview
- 2 Cache
- 2.1 basecache
- 2.2 databasecache
- 2.3 dummycache
- 2.4 filebasecache
- 2.5 locamemcache
- 2.6 memcachedcache
1 Overview
Some notes for reading Django source code when needed. All notes are based onDjango v1.0.4.
2 cache2.1 basecache
The cache in Django is an independent component that defines a parent class.Django/CORE/Cache/backend/base. pyThis module contains a parent classBasecache, Specified some basic operations.
- _ Init __: The expiration time of the cache item is specified for initialization, but different interpretations are provided based on the specific implementation.(For example, in database implementation, data items are deleted)
- Add: Add cache. The parameters areKey,Value,Expiration time(The default value is 300 seconds.). This method is implemented by sub-classes.
- Get: Get the cache. The parameter isKey,Default Value(The default value is none.).
- Set: Changes the cache. The parameters are the same.AddFunction.
- Delete: Deletes the cache. The parameter isKey.
- Get_many: Gets multiple caches. The parameter isKey List.
- Has_key: Whether a key exists. The parameter isKey.
- _ Contains __: Determines whether a function exists. The parameter isKey. Result andHas_keyConsistent.
2.2 databasecache
Database-based Cache. Module files are stored inDjango/CORE/Cache/backend/DB. pyThis file contains a database cache implementation that inherits basecache. It also specifies the maximum number of cache items stored in the database.
Note the following points:
- Use pickle to serialize objects. Therefore, it is acceptable to store any objects and will be converted to strings supported by the database.
- The expiration time is in seconds.
- Each timeSetWill process serialization, even if it is stored as a string. This is obviously not cost-effective.
- You can specifyCull_frequencyParameter, which is used as a reference for deletion when the quota is exceeded. The default value is3That is to say, when the capacity limit is exceeded, 1/3 of the cached data items are deleted.Cache_key, Instead of the expiration time, this may causeThe cache with a long validity period is removed early.Now.
- Expiration is performed every time a request is obtained. That is to say, some items may always exist in the Database .(After expiration, its cache_key is very large and rarely deleted.)
2.3 dummycache
The cache used for testing. The module file is inDjango/CORE/Cache/backend/dummy. py. This cache function is quite limited and can only be used for testing. It does not have any logic and only returns some default values.
2.4 filebasecache
File-based Cache. Module files are stored inDjango/CORE/Cache/backend/filebase. pyThis file system caches a file based on each cached object. It only uses some necessary methods to process multi-file management.
- The first path address passed during the construction is the path address.
- AddThe method will not succeed if an existing key is added, which is inconsistent with our common logic. If you need to callAddYou must specify a value. Therefore, if you add an existing key, you must add a judgment.
- Each timeSetA directory traversal is performed to search all files to see if the total number is exceeded.
- The file name is generatedCache_keyPerform MD5 once, and then take the two first and second characters After encryption as a level-2 Directory for storage (For example, AB/CD /*****In this way, the efficiency is very low because the index file is very slow when there are too many files in the directory. However, according to the maximum cache items accepted by the constructor, the default300It cannot constitute a threat to slow file indexing.FilebasecacheBy default, the second-level directory is used to store at least 1000*1000 cached data (Assuming that the cache files are evenly distributed). Whether the second-level directory is enabled only after a certain order of magnitude, or a first-level Directory management is added, or no directory is set by default.
2.5 locamemcache
Memory-based Cache, module files are stored inDjango/CORE/Cache/backend/locamem. py. Implemented a simple memory cache system.
The implementation of the lock memory cache system, but some flaws, it seems to be to ensure the overall unification, but this kind of unification is inevitably criticized.
- During the construction, two dictionaries are declared internally, one storing the cached value after serialization, the other storing the set expiration time, and the other declaring an asynchronous lock. However, as far as I knowPythonMultithreading is just masturbation,GilAs a result, you cannot let it go. Although the interpreter environment is more secure and stablePseudo-MultithreadingIt is difficult to mention interest.
- When the cache is added, the cache value is serialized.DictMaybe the only feeling is envy and hate.
- After the Maximum Cache value is exceeded,LocamemcacheAll_ Cull_frequencyThe modulo value is 0 only for use.Generator expression?! If_ Cull_frequencyIf the value is 0, the cache database is cleared._ Cull_frequencyIf the value is 1, the cache is cleared.
2.6 memcachedcache
In the memcached-based Cache System, module files are stored inDjango/CORE/Cache/backend/memcached. py. Simple encapsulation of cmemcache or memcache.
Django is finally clever and not serialized here, because these actions will be performed in the memcache library.
- All incomingUnicode charactersConvertedUTF-8, The string obtained from the cache is convertedUnicode characters.
- IfSetOrAddSpecify the expiration time0If it is not expired, it may be invalid becauseMemcachedcacheWhen processing the default expiration time,Logic orTherefore, if0Logically operatedPass Default Value.
- He has an additional method for closing the connection (CloseBy default, it is not called by itself, and needs to be manually called after each use.