o 3aR(@sdZddlZddlZddlmZddlmZGdddeZGdddeZ Gd d d e Z e Z d Zd d ZddZGdddZddZdS)zBase Cache class.N)ImproperlyConfigured) import_stringc@ eZdZdS)InvalidCacheBackendErrorN__name__ __module__ __qualname__r r A/usr/lib/python3/dist-packages/django/core/cache/backends/base.pyr rc@r)CacheKeyWarningNrr r r r r r r c@r)InvalidCacheKeyNrr r r r rr rcCsd|||fS)z Default function to generate keys. Construct the key used by all other methods. By default, prepend the `key_prefix`. KEY_FUNCTION can be used to specify an alternate function with custom key making behavior. z%s:%s:%sr )key key_prefixversionr r r default_key_funcsrcCs |durt|r |St|StS)z] Function to decide which key function to use. Default to ``default_key_func``. N)callablerr)key_funcr r r get_key_func(s rc@seZdZeZddZefddZd-ddZedfdd Z d.d d Z edfd d Z edfddZ d-ddZ d-ddZedfddZd-ddZd/ddZd/ddZddZedfdd Zd-d!d"Zd#d$Zd%d&Zd/d'd(Zd/d)d*Zd+d,ZdS)0 BaseCachec Cs|d|dd}|dur"zt|}Wn ttfy!d}Ynw||_|di}|d|dd}zt||_WnttfyJd|_Ynw|d|dd }zt||_Wnttfyjd |_Ynw|d d |_|d d |_t |d|_ dS)NtimeoutTIMEOUTi,OPTIONS max_entries MAX_ENTRIEScull_frequencyCULL_FREQUENCY KEY_PREFIXVERSION KEY_FUNCTION) getint ValueError TypeErrordefault_timeout _max_entries_cull_frequencyrrrr)selfparamsroptionsrrr r r __init__9s0    zBaseCache.__init__cCs4|tkr|j}n|dkrd}|durdSt|S)zj Return the timeout value usable by this backend based upon the provided timeout. rN)DEFAULT_TIMEOUTr)time)r,rr r r get_backend_timeoutSs zBaseCache.get_backend_timeoutNcCs|dur|j}|||j|S)ap Construct the key used by all other methods. By default, use the key_func to generate a key (which, by default, prepends the `key_prefix' and 'version'). A different key function can be provided at the time of cache construction; alternatively, you can subclass the cache backend to provide custom key making behavior. N)rrrr,rrr r r make_key_szBaseCache.make_keycCtd)z Set a value in the cache if the key does not already exist. If timeout is given, use that timeout for the key; otherwise use the default cache timeout. Return True if the value was stored, False otherwise. z4subclasses of BaseCache must provide an add() methodNotImplementedErrorr,rvaluerrr r r addlsz BaseCache.addcCr6)z Fetch a given key from the cache. If the key does not exist, return default, which itself defaults to None. z3subclasses of BaseCache must provide a get() methodr7)r,rdefaultrr r r r%vz BaseCache.getcCr6)z Set a value in the cache. If timeout is given, use that timeout for the key; otherwise use the default cache timeout. z3subclasses of BaseCache must provide a set() methodr7r9r r r set}r=z BaseCache.setcCr6)z Update the key's expiry time using timeout. Return True if successful or False if the key does not exist. z5subclasses of BaseCache must provide a touch() methodr7)r,rrrr r r touchr=zBaseCache.touchcCr6)zh Delete a key from the cache and return whether it succeeded, failing silently. z6subclasses of BaseCache must provide a delete() methodr7r4r r r deleter=zBaseCache.deletecCs6i}|D]}|j||j|d}||jur|||<q|S)a. Fetch a bunch of keys from the cache. For certain backends (memcached, pgsql) this can be *much* faster when fetching multiple values. Return a dict mapping each key in keys to its value. If the given key is missing, it will be missing from the response dict. rr% _missing_key)r,keysrdkvalr r r get_manys zBaseCache.get_manycCsP|j||j|d}||jur&t|r|}|j||||d|j|||dS|S)aU Fetch a given key from the cache. If the key does not exist, add the key and set it to the default value. The default value can also be any callable. If timeout is given, use that timeout for the key; otherwise use the default cache timeout. Return the value of the key stored or retrieved. rArr)r%rCrr;)r,rr<rrrGr r r get_or_sets zBaseCache.get_or_setcCs|j||j|d|juS)M Return True if the key is in the cache and has not expired. rArBr4r r r has_keyszBaseCache.has_keyr#cCsD|j||j|d}||jurtd|||}|j|||d|S)zs Add delta to value in the cache. If the key does not exist, raise a ValueError exception. rAKey '%s' not found)r%rCr'r>)r,rdeltarr: new_valuer r r incrs   zBaseCache.incrcCs|j|| |dS)zz Subtract delta from value in the cache. If the key does not exist, raise a ValueError exception. rA)rPr,rrNrr r r decrszBaseCache.decrcCs ||S)rK)rL)r,rr r r __contains__s zBaseCache.__contains__cCs(|D] \}}|j||||dqgS)a Set a bunch of values in the cache at once from a dict of key/value pairs. For certain backends (memcached), this is much more efficient than calling set() multiple times. If timeout is given, use that timeout for the key; otherwise use the default cache timeout. On backends that support it, return a list of keys that failed insertion, or an empty list if all keys were inserted successfully. rI)itemsr>)r,datarrrr:r r r set_manys zBaseCache.set_manycCs|D] }|j||dqdS)z Delete a bunch of values in the cache at once. For certain backends (memcached), this is much more efficient than calling delete() multiple times. rAN)r@)r,rDrrr r r delete_manyszBaseCache.delete_manycCr6)z+Remove *all* values from the cache at once.z5subclasses of BaseCache must provide a clear() methodr7)r,r r r clearszBaseCache.clearcCst|D]}t|tqdS)z Warn about keys that would not be portable to the memcached backend. This encourages (but does not force) writing backend-portable cache code. N)memcache_key_warningswarningswarnr )r,rwarningr r r validate_keys zBaseCache.validate_keycCs`|dur|j}|j||j|d}||jurtd||j||||d|j||d||S)zf Add delta to the cache version for the supplied key. Return the new version. NrArM)rr%rCr'r>r@)r,rrNrr:r r r incr_versions  zBaseCache.incr_versioncCs||| |S)zm Subtract delta from the cache version for the supplied key. Return the new version. )r^rQr r r decr_version szBaseCache.decr_versioncKsdS)zClose the cache connectionNr )r,kwargsr r r closeszBaseCache.close)N)NN)r#N)rrr objectrCr/r1r3r5r;r%r>r?r@rHrJrLrPrRrSrVrWrXr]r^r_rar r r r r6s.            rccsRt|tkrd|tfV|D]}t|dkst|dkr&d|VdSqdS)NzGCache key will cause errors if used with memcached: %r (longer than %s)!zOCache key contains characters that will cause errors if used with memcached: %r)lenMEMCACHE_MAX_KEY_LENGTHord)rcharr r r rYs rY)__doc__r2rZdjango.core.exceptionsrdjango.utils.module_loadingrrRuntimeWarningr r'rrbr1rfrrrrYr r r r s    c