o
    Ö]¼j0  ã                   @  sÔ   U d Z ddlmZ ddlZddlZddlmZmZmZ ddl	m
Z
 ddlmZmZ ddlmZmZ e d¡Zed	ƒZG d
d„ dee ƒZdaded< e ¡ Zdaded< e ¡ Zdddd„Zdddd„Zg d¢ZdS )zxIn-memory TTLCache implementation using cachetools.

Provides a thread-safe, TTL-based cache for storing API responses.
é    )ÚannotationsN)ÚAnyÚGenericÚTypeVar)ÚTTLCache)ÚSettingsÚget_settings)ÚDEFAULT_CACHE_TTL_SÚDEFAULT_EXPIRY_CACHE_TTL_Szindiaopt.cacheÚTc                   @  sR   e Zd ZdZddd
d„Zddd„Zddd„Zd dd„Zd!dd„Ze	d"dd„ƒZ
dS )#ÚMemoryCachezáThread-safe TTL in-memory cache.

    Wraps :class:`cachetools.TTLCache` with a thread lock.

    Args:
        maxsize: Maximum number of items the cache can hold.
        ttl:     Time-to-live for each item in seconds.
    éè  ç      N@ÚmaxsizeÚintÚttlÚfloatÚreturnÚNonec                 C  s"   t ||d�| _t ¡ | _|| _d S )N©r   r   )r   Ú_cacheÚ	threadingÚLockÚ_lockr   )Úselfr   r   © r   úX/home/dinkstrade/pdmp-scanner/venv/lib/python3.10/site-packages/indiaopt/cache/memory.pyÚ__init__#   s   

zMemoryCache.__init__ÚkeyÚstrúT | Nonec                 C  s6   | j � | j |¡W  d  ƒ S 1 sw   Y  dS )zCGet an item from the cache. Returns ``None`` if expired or missing.N)r   r   Úget©r   r   r   r   r   r!   (   s   
$ÿzMemoryCache.getÚvaluer   c                 C  s6   | j � || j|< W d  ƒ dS 1 sw   Y  dS )zSet an item in the cache.N)r   r   )r   r   r#   r   r   r   Úset-   ó   "ÿzMemoryCache.setc                 C  s:   | j � | j |d¡ W d  ƒ dS 1 sw   Y  dS )zDelete an item from the cache.N)r   r   Úpopr"   r   r   r   Údelete2   s   "ÿzMemoryCache.deletec                 C  s6   | j � | j ¡  W d  ƒ dS 1 sw   Y  dS )zClear all items from the cache.N)r   r   Úclear©r   r   r   r   r(   7   r%   zMemoryCache.clearc                 C  s6   | j � t| jjƒW  d  ƒ S 1 sw   Y  dS )z%Current number of items in the cache.N)r   r   r   Úcurrsizer)   r   r   r   r*   <   s   
$ÿzMemoryCache.currsizeN)r   r   )r   r   r   r   r   r   )r   r   r   r    )r   r   r#   r   r   r   )r   r   r   r   )r   r   )r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r$   r'   r(   Úpropertyr*   r   r   r   r   r      s    	



r   zMemoryCache[Any] | NoneÚ_chain_cachezMemoryCache[list[str]] | NoneÚ_expiry_cacheÚsettingsúSettings | Noner   úMemoryCache[Any]c                 C  ód   t �& tdu r | ptƒ }t|dtƒ}td|d�aW d  ƒ tS W d  ƒ tS 1 s+w   Y  tS )z)Return the global option-chain TTL cache.NÚ	cache_ttlr   r   )Ú_chain_cache_lockr0   r   Úgetattrr	   r   ©r2   Úsr   r   r   r   Úget_chain_cacheK   ó   

üü
ÿûr;   úMemoryCache[list[str]]c                 C  r5   )z)Return the global expiry-dates TTL cache.NÚexpiry_cache_ttliô  r   )Ú_expiry_cache_lockr1   r   r8   r
   r   r9   r   r   r   Úget_expiry_cacheV   r<   r@   )r   r;   r@   )N)r2   r3   r   r4   )r2   r3   r   r=   )r.   Ú
__future__r   Úloggingr   Útypingr   r   r   Ú
cachetoolsr   Úindiaopt.config.settingsr   r   Úindiaopt.constantsr	   r
   Ú	getLoggerÚloggerr   r   r0   Ú__annotations__r   r7   r1   r?   r;   r@   Ú__all__r   r   r   r   Ú<module>   s$    
+