
    h)                         d dl Z d dlZd dlZd dlmZmZ d dlmZmZ d dl	m
Z
 d dlmZmZmZ d dlmZmZ  G d de
      Zej&                  Zg d	Zg d
ZdZdZdZdZ G d de      Z G d de      Zy)    N)ABCabstractmethod)OrderedDictdefaultdict)Enum)ListSequenceUnion)KeyT	ResponseTc                       e Zd ZdZdZdZy)EvictionPolicylrulfurandomN)__name__
__module____qualname__LRULFURANDOM     W/var/www/html/retail-simulation-api/retail/lib/python3.12/site-packages/redis/_cache.pyr   r      s    
C
CFr   r   )9zBF.CARDzBF.DEBUGz	BF.EXISTSzBF.INFOz
BF.MEXISTSzBF.SCANDUMPz
CF.COMPACTzCF.COUNTzCF.DEBUGz	CF.EXISTSzCF.INFOz
CF.MEXISTSzCF.SCANDUMPzCMS.INFOz	CMS.QUERYDUMP
EXPIRETIMEzFT.AGGREGATEzFT.ALIASADDzFT.ALIASDELzFT.ALIASUPDATEz	FT.CURSORz
FT.EXPLAINzFT.EXPLAINCLIzFT.GETzFT.INFOzFT.MGETz
FT.PROFILEz	FT.SEARCHzFT.SPELLCHECKz	FT.SUGGETz	FT.SUGLENz
FT.SYNDUMPz
FT.TAGVALSzFT._ALIASADDIFNXzFT._ALIASDELIFX
HRANDFIELDz
JSON.DEBUGPEXPIRETIMEPFCOUNTPTTLSRANDMEMBERzTDIGEST.BYRANKzTDIGEST.BYREVRANKzTDIGEST.CDFzTDIGEST.INFOzTDIGEST.MAXzTDIGEST.MINzTDIGEST.QUANTILEzTDIGEST.RANKzTDIGEST.REVRANKzTDIGEST.TRIMMED_MEANz	TOPK.INFOz	TOPK.LISTz
TOPK.QUERYTOUCHTTL)JBITCOUNTBITFIELD_ROBITPOSEXISTSGEODISTGEOHASHGEOPOSGEORADIUSBYMEMBER_ROGEORADIUS_RO	GEOSEARCHGETGETBITGETRANGEHEXISTSHGETHGETALLHKEYSHLENHMGETHSTRLENHVALSzJSON.ARRINDEXzJSON.ARRLENzJSON.GETz	JSON.MGETzJSON.OBJKEYSzJSON.OBJLENz	JSON.RESPzJSON.STRLENz	JSON.TYPELCSLINDEXLLENLPOSLRANGEMGETSCARDSDIFFSINTER
SINTERCARD	SISMEMBERSMEMBERS
SMISMEMBERSORT_ROSTRLENSUBSTRSUNIONzTS.GETzTS.INFOzTS.RANGEzTS.REVRANGETYPEXLENXPENDINGXRANGEXREAD	XREVRANGEZCARDZCOUNTZDIFFZINTER
ZINTERCARD	ZLEXCOUNTZMSCOREZRANGEZRANGEBYLEXZRANGEBYSCOREZRANK	ZREVRANGEZREVRANGEBYLEXZREVRANGEBYSCOREZREVRANKZSCOREZUNIONresponsekeysctimeaccess_countc                       e Zd ZdZedeeee   f   dede	e
   fd       Zedeeee   f   defd       Zedeeee   f   fd       Zed	e	eeee   f      fd
       Zed        Zede
fd       Zy)AbstractCachez
    An abstract base class for client caching implementations.
    If you want to implement your own cache you must support these methods.
    commandra   keys_in_commandc                      y Nr   selfrg   ra   rh   s       r   setzAbstractCache.set   s     	r   returnc                      y rj   r   rl   rg   s     r   getzAbstractCache.get       r   c                      y rj   r   rp   s     r   delete_commandzAbstractCache.delete_command   rr   r   commandsc                      y rj   r   )rl   ru   s     r   delete_commandszAbstractCache.delete_commands   rr   r   c                      y rj   r   rl   s    r   flushzAbstractCache.flush   rr   r   keyc                      y rj   r   )rl   r{   s     r   invalidate_keyzAbstractCache.invalidate_key   rr   r   N)r   r   r   __doc__r   r
   strr	   r   r   r   rm   rq   rt   rw   rz   r}   r   r   r   rf   rf      s    
 sHSM)*  d	  5hsm!34    eC#,>&?   U33E-F(G     $  r   rf   c                   T   e Zd ZdZddefdededefdZdee	e
e	   f   d	ed
ee   fdZdee	e
e	   f   defdZdee	e
e	   f   fdZdeee	e
e	   f      fdZd Zdee	e
e	   f   defdZdee	e
e	   f   fdZd Zdee   dee	e
e	   f   fdZdee   dee	e
e	   f   fdZdefdZy)_LocalCachea  
    A caching mechanism for storing redis commands and their responses.

    Args:
        max_size (int): The maximum number of commands to be stored in the cache.
        ttl (int): The time-to-live for each command in seconds.
        eviction_policy (EvictionPolicy): The eviction policy to use for removing commands when the cache is full.

    Attributes:
        max_size (int): The maximum number of commands to be stored in the cache.
        ttl (int): The time-to-live for each command in seconds.
        eviction_policy (EvictionPolicy): The eviction policy used for cache management.
        cache (OrderedDict): The ordered dictionary to store commands and their metadata.
        key_commands_map (defaultdict): A mapping of keys to the set of commands that use each key.
        commands_ttl_list (list): A list to keep track of the commands in the order they were added.  # noqa
    i'  r   max_sizettleviction_policyc                     || _         || _        || _        t               | _        t        t              | _        g | _        y rj   )	r   r   r   r   cacher   rm   key_commands_mapcommands_ttl_list)rl   r   r   r   s       r   __init__z_LocalCache.__init__   s8     !. ]
 +C 0!#r   rg   ra   rh   c                 4   t        | j                        | j                  k\  r| j                          t        |t
        |t        t        j                         t        di| j                  |<   | j                  ||       | j                  j                  |       y)a3  
        Set a redis command and its response in the cache.

        Args:
            command (Union[str, Sequence[str]]): The redis command.
            response (ResponseT): The response associated with the command.
            keys_in_command (List[KeyT]): The list of keys used in the command.
        r   N)lenr   r   _evict	_RESPONSE_KEYS_CTIMEtime	monotonic_ACCESS_COUNT_update_key_commands_mapr   appendrk   s       r   rm   z_LocalCache.set   sr     tzz?dmm+KKMx?DNN$1	


7 	%%ow?%%g.r   rn   c                     || j                   v rY| j                  |      r| j                  |       y| j                  |       t	        j
                  | j                   |   d         S y)a   
        Get the response for a redis command from the cache.

        Args:
            command (Union[str, Sequence[str]]): The redis command.

        Returns:
            ResponseT: The response associated with the command, or None if the command is not in the cache.  # noqa
        Nra   )r   _is_expiredrt   _update_accesscopydeepcopyrp   s     r   rq   z_LocalCache.get   s_     djj (##G,(==G!4Z!@AA !r   c                     || j                   v rY| j                   |   j                  d      }| j                  ||       | j                  j	                  |       | j                   |= yy)z
        Delete a redis command and its metadata from the cache.

        Args:
            command (Union[str, Sequence[str]]): The redis command to be deleted.
        rb   N)r   rq   _del_key_commands_mapr   remove)rl   rg   rh   s      r   rt   z_LocalCache.delete_command  s^     djj "jj155f=O&&@""))'2

7#	 !r   ru   c                 4    |D ]  }| j                  |        y)z
        Delete multiple commands and their metadata from the cache.

        Args:
            commands (List[Union[str, Sequence[str]]]): The list of commands to be
            deleted.
        N)rt   )rl   ru   rg   s      r   rw   z_LocalCache.delete_commands  s      G(  r   c                 z    | j                   j                          | j                  j                          g | _        y)zAClear the entire cache, removing all redis commands and metadata.N)r   clearr   r   ry   s    r   rz   z_LocalCache.flush(  s,    

##%!#r   c                     | j                   dk(  ryt        j                         | j                  |   d   z
  | j                   kD  S )z
        Check if a redis command has expired based on its time-to-live.

        Args:
            command (Union[str, Sequence[str]]): The redis command.

        Returns:
            bool: True if the command has expired, False otherwise.
        r   Frc   )r   r   r   r   rp   s     r   r   z_LocalCache._is_expired.  s;     88q=~~$**W"5g">>IIr   c                    | j                   t        j                  k(  r| j                  j	                  |       y| j                   t        j
                  k(  r[| j                  j                  |i       j                  dd      dz   | j                  |   d<   | j                  j	                  |       y| j                   t        j                  k(  ryy)z
        Update the access information for a redis command based on the eviction policy.

        Args:
            command (Union[str, Sequence[str]]): The redis command.
        rd   r      N)r   r   r   r   move_to_endr   rq   r   rp   s     r   r   z_LocalCache._update_access<  s     >#5#55JJ""7+!!^%7%77

w+//BQF JJw/ JJ""7+!!^%:%:: ;r   c                 x     j                   j                  d         r j                   j                  d          y j                  t        j
                  k(  r j                  j                  d       y j                  t        j                  k(  r6t         j                   fd      } j                  j                  |       y j                  t        j                  k(  rRt        j                  t         j                  j                                     } j                  j                  |       yy)zBEvict a redis command from the cache based on the eviction policy.r   F)lastc                 B    j                   |    j                  dd      S )Nrd   r   )r   rq   )krl   s    r   <lambda>z$_LocalCache._evict.<locals>.<lambda>U  s    $**Q-*;*;NA*Nr   )r{   N)r   r   rt   r   r   r   r   popitemr   minpopr   r   choicelistrb   )rl   min_access_commandrandom_commands   `  r   r   z_LocalCache._evictM  s    D22156 6 6q 9:!!^%7%77JJE*!!^%7%77!$

 N" JJNN-.!!^%:%::#]]4

0A+BCNJJNN>* ;r   rb   c                 N    |D ]   }| j                   |   j                  |       " y)z
        Update the key_commands_map with command that uses the keys.

        Args:
            keys (List[KeyT]): The list of keys used in the command.
            command (Union[str, Sequence[str]]): The redis command.
        N)r   addrl   rb   rg   r{   s       r   r   z$_LocalCache._update_key_commands_map\  s'     C!!#&**73 r   c                 N    |D ]   }| j                   |   j                  |       " y)z
        Remove a redis command from the key_commands_map.

        Args:
            keys (List[KeyT]): The list of keys used in the redis command.
            command (Union[str, Sequence[str]]): The redis command.
        N)r   r   r   s       r   r   z!_LocalCache._del_key_commands_mapi  s'     C!!#&--g6 r   r{   c                     || j                   vryt        | j                   |         }|D ]  }| j                  |        y)z
        Invalidate (delete) all redis commands associated with a specific key.

        Args:
            key (KeyT): The key to be invalidated.
        N)r   r   rt   )rl   r{   ru   rg   s       r   r}   z_LocalCache.invalidate_keyv  sB     d+++--c23G(  r   N)r   r   r   r~   DEFAULT_EVICTION_POLICYintr   r   r
   r   r	   r   r   r   rm   rq   rt   rw   rz   boolr   r   r   r   r   r}   r   r   r   r   r      se   & *A	$$ $ (	$/sHSM)*/ / d	/2B5hsm!34 B B"$eC#,>&? $	)U33E-F(G 	)$J5hsm);#< J JeC#,>&? "+4J4).sHSM/A)B47J7).sHSM/A)B7)$ )r   r   )r   r   r   abcr   r   collectionsr   r   enumr   typingr   r	   r
   redis.typingr   r   r   r   r   DEFAULT_DENY_LISTDEFAULT_ALLOW_LISTr   r   r   r   rf   r   r   r   r   <module>r      s~       # 0  ( ( (T  ),, : xK Z 		!C !Hz)- z)r   