
    sg>                     ~    d Z ddlmZ ddlmZ 	 ddlmZ ddlmZm	Z	m
Z
 ddlmZ  G d	 d
e      Zy# e$ r dZY %w xY w)-Django-compatible disk and file backed cache.    )wraps)	BaseCache)DEFAULT_TIMEOUTi,     )ENOVALargs_to_key	full_name)FanoutCachec                       e Zd ZdZ fdZed        Zd ZddZd Z	e
dddd	fd
Z	 	 	 	 	 	 d dZddZe
dddd	fdZe
dd	fdZ	 	 	 	 	 d!dZd"dZd#dZd#dZddZd Zd$dZd Zd Zd Zd Zd Zd Ze
fdZde
ddddfdZ xZS )%DjangoCacher   c                     t         |   |       |j                  dd      }|j                  dd      }|j                  di       }t        |||fi || _        y)zInitialize DjangoCache instance.

        :param str directory: cache directory
        :param dict params: cache parameters

        SHARDS   DATABASE_TIMEOUTg{Gz?OPTIONSN)super__init__getr   _cache)self	directoryparamsshardstimeoutoptions	__class__s         H/var/www/html/venv/lib/python3.12/site-packages/diskcache/djangocache.pyr   zDjangoCache.__init__   sX     	 Ha(**/7**Y+!)VWHH    c                 .    | j                   j                  S )zCache directory.)r   r   r   s    r   r   zDjangoCache.directory!   s     {{$$$r   c                 8    | j                   j                  |      S )zReturn Cache with given `name` in subdirectory.

        :param str name: subdirectory name for Cache
        :return: Cache with given name

        )r   cacher   names     r   r#   zDjangoCache.cache&        {{  &&r   Nc                 <    | j                   j                  ||      S )zReturn Deque with given `name` in subdirectory.

        :param str name: subdirectory name for Deque
        :param maxlen: max length (default None, no max)
        :return: Deque with given name

        )maxlen)r   deque)r   r%   r(   s      r   r)   zDjangoCache.deque/   s     {{  f 55r   c                 8    | j                   j                  |      S )zReturn Index with given `name` in subdirectory.

        :param str name: subdirectory name for Index
        :return: Index with given name

        )r   indexr$   s     r   r+   zDjangoCache.index9   r&   r   FTc                     | j                  ||      }| j                  |      }| j                  j                  ||||||      S )a  Set a value in the cache if the key does not already exist. If
        timeout is given, that timeout will be used for the key; otherwise the
        default cache timeout will be used.

        Return True if the value was stored, False otherwise.

        :param key: key for item
        :param value: value for item
        :param float timeout: seconds until the item expires
            (default 300 seconds)
        :param int version: key version number (default None, cache parameter)
        :param bool read: read value as bytes from file (default False)
        :param str tag: text to associate with key (default None)
        :param bool retry: retry if database timeout occurs (default True)
        :return: True if item was added

        versionr   )make_keyget_backend_timeoutr   addr   keyvaluer   r.   readtagretrys           r   r2   zDjangoCache.addB   sF    8 mmCm1**7*;{{sE7D#uEEr   c                 h    | j                  ||      }| j                  j                  ||||||      S )a  Fetch a given key from the cache. If the key does not exist, return
        default, which itself defaults to None.

        :param key: key for item
        :param default: return value if key is missing (default None)
        :param int version: key version number (default None, cache parameter)
        :param bool read: if True, return file handle to value
            (default False)
        :param float expire_time: if True, return expire_time in tuple
            (default False)
        :param tag: if True, return tag in tuple (default False)
        :param bool retry: retry if database timeout occurs (default False)
        :return: value for item if key is found else default

        r-   )r0   r   r   )r   r4   defaultr.   r6   expire_timer7   r8   s           r   r   zDjangoCache.getb   s3    4 mmCm1{{sGT;UKKr   c                 ^    | j                  ||      }| j                  j                  |      S )a  Return file handle corresponding to `key` from Cache.

        :param key: Python key to retrieve
        :param int version: key version number (default None, cache parameter)
        :return: file open for reading in binary mode
        :raises KeyError: if key is not found

        r-   )r0   r   r6   r   r4   r.   s      r   r6   zDjangoCache.read   s+     mmCm1{{$$r   c                     | j                  ||      }| j                  |      }| j                  j                  ||||||      S )a  Set a value in the cache. If timeout is given, that timeout will be
        used for the key; otherwise the default cache timeout will be used.

        :param key: key for item
        :param value: value for item
        :param float timeout: seconds until the item expires
            (default 300 seconds)
        :param int version: key version number (default None, cache parameter)
        :param bool read: read value as bytes from file (default False)
        :param str tag: text to associate with key (default None)
        :param bool retry: retry if database timeout occurs (default True)
        :return: True if item was set

        r-   r/   )r0   r1   r   setr3   s           r   r?   zDjangoCache.set   sF    2 mmCm1**7*;{{sE7D#uEEr   c                     | j                  ||      }| j                  |      }| j                  j                  |||      S )a  Touch a key in the cache. If timeout is given, that timeout will be
        used for the key; otherwise the default cache timeout will be used.

        :param key: key for item
        :param float timeout: seconds until the item expires
            (default 300 seconds)
        :param int version: key version number (default None, cache parameter)
        :param bool retry: retry if database timeout occurs (default True)
        :return: True if key was touched

        r-   r/   )r0   r1   r   touch)r   r4   r   r.   r8   s        r   rA   zDjangoCache.touch   sB     mmCm1**7*;{{  gu55r   c                 f    | j                  ||      }| j                  j                  |||||      S )a  Remove corresponding item for `key` from cache and return value.

        If `key` is missing, return `default`.

        Operation is atomic. Concurrent operations will be serialized.

        :param key: key for item
        :param default: return value if key is missing (default None)
        :param int version: key version number (default None, cache parameter)
        :param float expire_time: if True, return expire_time in tuple
            (default False)
        :param tag: if True, return tag in tuple (default False)
        :param bool retry: retry if database timeout occurs (default True)
        :return: value for item if key is found else default

        r-   )r0   r   pop)r   r4   r:   r.   r;   r7   r8   s          r   rC   zDjangoCache.pop   s1    2 mmCm1{{sG[#uEEr   c                 `    | j                  ||      }| j                  j                  ||      S )a  Delete a key from the cache, failing silently.

        :param key: key for item
        :param int version: key version number (default None, cache parameter)
        :param bool retry: retry if database timeout occurs (default True)
        :return: True if item was deleted

        r-   )r0   r   delete)r   r4   r.   r8   s       r   rE   zDjangoCache.delete   s-     mmCm1{{!!#u--r   c                     | j                  ||      }	 | j                  j                  ||||      S # t        $ r t	        d|z        dw xY w)a  Increment value by delta for item with key.

        If key is missing and default is None then raise KeyError. Else if key
        is missing and default is not None then use default for value.

        Operation is atomic. All concurrent increment operations will be
        counted individually.

        Assumes value may be stored in a SQLite column. Most builds that target
        machines with 64-bit pointer widths will support 64-bit signed
        integers.

        :param key: key for item
        :param int delta: amount to increment (default 1)
        :param int version: key version number (default None, cache parameter)
        :param int default: value if key is missing (default None)
        :param bool retry: retry if database timeout occurs (default True)
        :return: new value for item on success else None
        :raises ValueError: if key is not found and default is None

        r-   zKey '%s' not foundN)r0   r   incrKeyError
ValueErrorr   r4   deltar.   r:   r8   s         r   rG   zDjangoCache.incr   s[    . mmCm1	C;;##C?? 	C1C78dB	Cs	   3 Ac                 .    | j                  || |||      S )a  Decrement value by delta for item with key.

        If key is missing and default is None then raise KeyError. Else if key
        is missing and default is not None then use default for value.

        Operation is atomic. All concurrent decrement operations will be
        counted individually.

        Unlike Memcached, negative values are supported. Value may be
        decremented below zero.

        Assumes value may be stored in a SQLite column. Most builds that target
        machines with 64-bit pointer widths will support 64-bit signed
        integers.

        :param key: key for item
        :param int delta: amount to decrement (default 1)
        :param int version: key version number (default None, cache parameter)
        :param int default: value if key is missing (default None)
        :param bool retry: retry if database timeout occurs (default True)
        :return: new value for item on success else None
        :raises ValueError: if key is not found and default is None

        )rG   rJ   s         r   decrzDjangoCache.decr   s    4 yyufgw>>r   c                 D    | j                  ||      }|| j                  v S )zReturns True if the key is in the cache and has not expired.

        :param key: key for item
        :param int version: key version number (default None, cache parameter)
        :return: True if key is found

        r-   )r0   r   r=   s      r   has_keyzDjangoCache.has_key  s%     mmCm1dkk!!r   c                 6    | j                   j                         S )zSRemove expired items from cache.

        :return: count of items removed

        )r   expirer!   s    r   rQ   zDjangoCache.expire&  s     {{!!##r   c                 <    | j                   j                  ||      S )zReturn cache statistics hits and misses.

        :param bool enable: enable collecting statistics (default True)
        :param bool reset: reset hits and misses to 0 (default False)
        :return: (hits, misses)

        )enablereset)r   stats)r   rS   rT   s      r   rU   zDjangoCache.stats.  s     {{  e <<r   c                 8    | j                   j                          y)zCreate tag index on cache database.

        Better to initialize cache with `tag_index=True` than use this.

        :raises Timeout: if database timeout occurs

        N)r   create_tag_indexr!   s    r   rW   zDjangoCache.create_tag_index8  s     	$$&r   c                 8    | j                   j                          y)z`Drop tag index on cache database.

        :raises Timeout: if database timeout occurs

        N)r   drop_tag_indexr!   s    r   rY   zDjangoCache.drop_tag_indexB  s     	""$r   c                 8    | j                   j                  |      S )zRemove items with matching `tag` from cache.

        :param str tag: tag identifying items
        :return: count of items removed

        )r   evict)r   r7   s     r   r[   zDjangoCache.evictJ  s     {{  %%r   c                 6    | j                   j                         S )znCull items from cache until volume is less than size limit.

        :return: count of items removed

        )r   cullr!   s    r   r]   zDjangoCache.cullS  s     {{!!r   c                 6    | j                   j                         S )z+Remove *all* values from the cache at once.)r   clearr!   s    r   r_   zDjangoCache.clear[  s    {{  ""r   c                 8    | j                   j                          y)zClose the cache connection.N)r   close)r   kwargss     r   ra   zDjangoCache.close_  s     	r   c                 H    |t         k(  r| j                  }n|dk(  rd}|dS |S )zReturn seconds to expiration.

        :param float timeout: seconds until the item expires
            (default 300 seconds)

        r   N)r   default_timeout)r   r   s     r   r1   zDjangoCache.get_backend_timeoutd  s2     o%**G\Gt3G3r    c                 V     t              rt        d       fd}|S )a  Memoizing cache decorator.

        Decorator to wrap callable with memoizing function using cache.
        Repeated calls with the same arguments will lookup result in cache and
        avoid function evaluation.

        If name is set to None (default), the callable name will be determined
        automatically.

        When timeout is set to zero, function results will not be set in the
        cache. Cache lookups still occur, however. Read
        :doc:`case-study-landing-page-caching` for example usage.

        If typed is set to True, function arguments of different types will be
        cached separately. For example, f(3) and f(3.0) will be treated as
        distinct calls with distinct results.

        The original underlying function is accessible through the __wrapped__
        attribute. This is useful for introspection, for bypassing the cache,
        or for rewrapping the function with a different cache.

        An additional `__cache_key__` attribute can be used to generate the
        cache key used for the given arguments.

        Remember to call memoize when decorating a callable. If you forget,
        then a TypeError will occur.

        :param str name: name given for callable (default None, automatic)
        :param float timeout: seconds until the item expires
            (default 300 seconds)
        :param int version: key version number (default None, cache parameter)
        :param bool typed: cache different types separately (default False)
        :param str tag: text to associate with arguments (default None)
        :param set ignore: positional or keyword args to ignore (default ())
        :return: callable decorator

        zname cannot be callablec                 |     t               fnft                
fd       	fd}|_        S )z3Decorator created by memoize() for callable `func`.c                       
j                   | i |}j                  |t        	d      }|t        u r7 | i |}du xs t        k(  xs dkD  }|rj	                  ||	d       |S )z:Wrapper for callable to cache arguments and return values.T)r8   Nr   )r7   r8   )__cache_key__r   r   r   r?   )argsrb   r4   resultvalid_timeoutfuncr   r7   r   r.   wrappers        r   ro   z7DjangoCache.memoize.<locals>.decorator.<locals>.wrapper  s     ,g++T<V<#vwdCV#!4262F4 '"o5'"Q; "
 %"## #"& !  r   c                  "    t        | |      S )z,Make key for cache given function arguments.)r	   )rk   rb   baseignoretypeds     r   rj   z=DjangoCache.memoize.<locals>.decorator.<locals>.__cache_key__  s    "4vufEEr   )r
   r   rj   )rn   rj   rq   ro   rr   r%   r   r7   r   rs   r.   s   ` @@r   	decoratorz&DjangoCache.memoize.<locals>.decorator  sG    )-IdO%D7D4[  0F %2G!Nr   )callable	TypeError)r   r%   r   r.   rs   r7   rr   rt   s   ``````` r   memoizezDjangoCache.memoizer  s,    ^ D>566"	 "	H r   )N)NNFFFF)NNFFT)NT)r   NNT)TF) __name__
__module____qualname____doc__r   propertyr   r#   r)   r+   r   r2   r   r6   r?   rA   rC   rE   rG   rM   rO   rQ   rU   rW   rY   r[   r]   r_   ra   r1   rw   __classcell__)r   s   @r   r   r      s   7I % %'6'  FF L:
%   F: "1$d 6( F8.C:?8	"$='%&"#
 +: 4  Vr   r   N)r{   	functoolsr   django.core.cache.backends.baser   r   ImportErrorcorer   r	   r
   fanoutr   r   rf   r   r   <module>r      sE    3  5?
 1 0 w) w  Os   2 <<