o 3a7@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z m Z ddl m Z ddlmZGddde ZdS)zFile-based cache backendN)DEFAULT_TIMEOUT BaseCache)locks)file_move_safecseZdZdZejZfddZedfddZ d!ddZ d d Z edfd d Z edfd dZ d"ddZddZd"ddZddZddZd"ddZddZddZdd ZZS)#FileBasedCachez.djcachecs&t|tj||_|dSN)super__init__ospathabspath_dir _createdir)selfdirparams __class__F/usr/lib/python3/dist-packages/django/core/cache/backends/filebased.pyr s  zFileBasedCache.__init__NcCs$|||rdS|||||dSNFT)has_keyset)rkeyvaluetimeoutversionrrradds zFileBasedCache.addcCs|||}z1t|d!}||s$tt|WdWSWdW|S1s0wYW|StyAY|Sw)Nrb) _key_to_fileopen _is_expiredpickleloadszlib decompressreadFileNotFoundError)rrdefaultrfnamefrrrgets     zFileBasedCache.getcCs<||}|t||j|tt||jdSr)get_backend_timeoutwriter"dumpspickle_protocolr$compress)rfilerrexpiryrrr_write_content)s zFileBasedCache._write_contentc Cs||||}|tj|jd\}}d}z1t|d} || ||Wdn1s1wYt||ddd}W|sIt |dSdS|sSt |ww)N)rFwbT)allow_overwrite) rr_culltempfilemkstempr r r3rr remove) rrrrrr)fdtmp_pathrenamedr*rrrr.s    zFileBasedCache.setc Csz\t|||dH}z@t|tj||r'Wt|WdWdStt | }| d| |||Wt|WdWdSt|w1sUwYWdStyfYdSw)Nzr+bFrT)r rrlockLOCK_EXr!unlockr"r#r$r%r&seekr3r')rrrrr*previous_valuerrrtouch=s&      zFileBasedCache.touchcCs||||Sr)_deleterrrrrrrdeleteNszFileBasedCache.deletecCsB||jr tj|sdSzt|WdSty YdSwr) startswithr r r existsr9r'rr)rrrrCQs  zFileBasedCache._deletecCsR|||}tj|r't|d}|| WdS1s"wYdS)NrF)rr r rGr r!)rrrr)r*rrrr[s     zFileBasedCache.has_keycCs^|}t|}||jkrdS|jdkr|St|t||j}|D]}||q%dS)z Remove random cache entries if max_entries is reached at a ratio of num_entries / cull_frequency. A value of 0 for CULL_FREQUENCY means that the entire cache will be purged. Nr) _list_cache_fileslen _max_entries_cull_frequencyclearrandomsampleintrC)rfilelist num_entriesr)rrrr6bs    zFileBasedCache._cullc Cs:td}ztj|jdddWt|dSt|w)N?iT)exist_ok)r umaskmakedirsr )r old_umaskrrrrts zFileBasedCache._createdirc CsB|j||d}||tj|jdt| |j gS)z Convert a key into a cache file path. Basically this is the root cache path joined with the md5sum of the key and a suffix. )r) make_key validate_keyr r joinr hashlibmd5encode hexdigest cache_suffixrDrrrr}s  zFileBasedCache._key_to_filecCs|D]}||qdS)z- Remove all the cache files. N)rIrCrHrrrrMs  zFileBasedCache.clearcCsVzt|}Wn tyd}Ynw|dur)|tkr)|||jdSdS)zL Take an open cache file `f` and delete it if it's expired. rNTF)r"loadEOFErrortimecloserCname)rr*exprrrr!s  zFileBasedCache._is_expiredcs"fddtjdjDS)z Get a list of paths to all the cache files. These are all the files in the root cache dir that end on the cache_suffix. csg|] }tjj|qSr)r r r[r ).0r)rrr sz4FileBasedCache._list_cache_files..z*%s)globglob1r r`rhrrhrrIs z FileBasedCache._list_cache_files)NNr)__name__ __module__ __qualname__r`r"HIGHEST_PROTOCOLr/r rrr+r3rrBrErCrr6rrrMr!rI __classcell__rrrrrs$       r)__doc__rjr\r r"rNr7rcr$django.core.cache.backends.baserrdjango.core.filesrdjango.core.files.moverrrrrrs