Skip to main content
Glama

Malaysia Prayer Time MCP Server

cache.cpython-311.pyc10.5 kB
� ÷�g��� �dZddlmZddlZddlZddlZddlmZddlm Z ddl m Z m Z m Z mZmZmZddlmZeje��Zed ��ZeGd �d ee����ZGd �d ��Ze��Zdd�Zddd�ZdS)aW Thread-safe in-memory cache implementation with TTL support. This module provides a simple but robust caching mechanism with the following features: - Thread-safe operations - TTL (Time To Live) support - Automatic cleanup of expired entries - Capacity management to prevent memory leaks - Decorator support for easy function result caching �)� annotationsN)� dataclass��wraps)�Dict�Any�Optional�TypeVar�Generic�Callable�)�config�Tc�(�eZdZUdZded<ded<dS)� CacheEntryz A cache entry with TTL tracking.r�value�float� expires_atN)�__name__� __module__� __qualname__�__doc__�__annotations__���P/Users/aman/2) personal/mcp-server-malaysia-prayer-time/src/waktu_solat/cache.pyrrs-�������*�*� �H�H�H������rrc�L�eZdZdZdd�Zdd�Zdd�Zdd �Zddd�Zdd�Z dd�Z d S)�Cachez-Thread-safe in-memory cache with TTL support.�return�Nonec��i|_tjj|_tjj|_tj��|_ dS)z Initialize a new cache instance.N) �_storer�cache�max_size� _max_size�ttl�_ttl� threading�RLock�_lock��selfs r�__init__zCache.__init__'s4��24�� �$�l�3�����)�� �&/�o�&7�&7�� � � rc����tj����fd�|j���D��}|r:|D] }|j|=� t�dt |���d���dSdS)z"Remove expired entries from cache.c�0��g|]\}}|j�k�|��Sr�r)�.0�key�entry�nows �r� <listcomp>z(Cache._clean_expired.<locals>.<listcomp>1s1��� � � ��C���9I�S�9P�9P�C�9P�9P�9PrzCleaned z expired entries from cacheN)�timer"�items�logger�debug�len)r,� expired_keysr2r4s @r�_clean_expiredzCache._clean_expired.s�����i�k�k�� � � � �"&�+�"3�"3�"5�"5� � � � � � T�#� %� %���K��$�$� �L�L�R�C� �$5�$5�R�R�R� S� S� S� S� S� T� Trc�2�t|j��|jkrytd|jdz��}t |j���d����}|d|�D] \}}|j|=�t �d|�d���dSdS)z@Ensure cache doesn't exceed max size by removing oldest entries.r � c��|djS)Nr r0)�xs r�<lambda>z(Cache._ensure_capacity.<locals>.<lambda>>s ��Q�q�T�_�r)r2NzRemoved z! entries to ensure cache capacity)r:r"r%�max�sortedr7r8r9)r,� num_to_remove� sorted_itemsr2�_s r�_ensure_capacityzCache._ensure_capacity9s��� �t�{� � �t�~� -� -���4�>�R�#7�8�8�M�!�$�+�"3�"3�"5�"5�;T�;T�U�U�U�L�&�~� �~�6� %� %���Q��K��$�$� �L�L�T�M�T�T�T� U� U� U� U� U� .� -rr2�str� Optional[Any]c���|j5|���|j�|��}|�+t�d|���� ddd��dS|jtj��kr3|j|=t�d|���� ddd��dSt�d|����|jcddd��S#1swxYwYdS)z� Get a value from cache. Args: key: The key to look up Returns: The cached value if it exists and hasn't expired, None otherwise NzCache miss for key: zCache entry expired for key: zCache hit for key: ) r*r<r"�getr8r9rr6r)r,r2r3s rrKz Cache.getCsh���Z� � � � � � !� !� !��K�O�O�C�(�(�E��}�� � �9�C�9�9�:�:�:��  � � � � � � � ���4�9�;�;�.�.��K��$�� � �B�S�B�B�C�C�C�� � � � � � � � � �L�L�4�s�4�4� 5� 5� 5��;� � � � � � � � � � � � ���� � � � � � s�AC#�$AC#�3#C#�#C'�*C'Nrrr&� Optional[int]c�x�|�|dkrtd���|j5|���|���|�|n|j}t j��|z}t ||��|j|<t� d|�d|�d���ddd��dS#1swxYwYdS)aD Set a value in cache with optional TTL override. Args: key: The key to store the value under value: The value to cache ttl: Optional TTL override in seconds. Must be positive if provided. Raises: ValueError: If TTL is not a positive integer NrzTTL must be a positive integerzCache set for key: z with TTL: �s) � ValueErrorr*r<rGr'r6rr"r8r9)r,r2rr&� effective_ttlrs r�setz Cache.set]s�� �?�s�a�x�x��=�>�>� >� �Z� Q� Q� � � � !� !� !� � !� !� #� #� #�#&�?�C�C�� �M�����}�4�J�)�%��<�<�D�K�� � �L�L�O�s�O�O�}�O�O�O� P� P� P� Q� Q� Q� Q� Q� Q� Q� Q� Q� Q� Q� Q���� Q� Q� Q� Q� Q� Qs�BB/�/B3�6B3c��|j5|j�|d���t�d|����ddd��dS#1swxYwYdS)z\ Remove a key from cache. Args: key: The key to remove NzDeleted cache entry for key: )r*r"�popr8r9)r,r2s r�deletez Cache.deleteus����Z� D� D��{���s�D�)�)�5�� � �B�S�B�B�C�C�C� D� D� D� D� D� D� D� D� D� D� D� D���� D� D� D� D� D� Ds�9A�A�Ac��|j5|j���t�d��ddd��dS#1swxYwYdS)zClear all entries from cache.z Cache clearedN)r*r"�clearr8r9r+s rrVz Cache.clear�s��� �Z� *� *� �K� � � � � � �L�L�� )� )� )� *� *� *� *� *� *� *� *� *� *� *� *���� *� *� *� *� *� *s�4A � A �A )rr )r2rHrrI�N)r2rHrrr&rLrr )r2rHrr ) rrrrr-r<rGrKrQrTrVrrrrr$s�������7�7�8�8�8�8� T� T� T� T�V�V�V�V�����4Q�Q�Q�Q�Q�0 D� D� D� D�*�*�*�*�*�*rr�argsr�kwargsrrHc���d�d�|D����}d�d�t|�����D����}|�d|���d��S)z*Generate a cache key from args and kwargs.rFc3�4K�|]}t|��V��dSrW)rH)r1�args r� <genexpr>zcache_key.<locals>.<genexpr>�s(����1�1�S��C���1�1�1�1�1�1rc3�*K�|]\}}|�d|��V��dS)�:Nr)r1�k�vs rr]zcache_key.<locals>.<genexpr>�s0����H�H���A�Q�*�*��*�*�H�H�H�H�H�Hr)�joinrCr7�strip)rXrY�args_str� kwargs_strs r� cache_keyrf�st���x�x�1�1�D�1�1�1�1�1�H����H�H��� � ���1G�1G�H�H�H�H�H�J�� %� %�� %� %� +� +�C� 0� 0�0rr&rLc���d�fd� }|S)z� Decorator for caching function results. Args: ttl: Optional TTL override in seconds Example: @cached(ttl=300) async def get_zones(): return await fetch_zones() �funcr c�@���t�����fd���}|S)Nc ����K��j�d�j�dt|i|����}t�|��}|�|S�|i|���d{V��}t�||���|S)N�.rF)rrrfr#rKrQ)rXrYr2�resultrhr&s ��r�wrapperz*cached.<locals>.decorator.<locals>.wrapper�s�������_�S�S�t�}�S�S�y�$�7Q�&�7Q�7Q�S�S�C��Y�Y�s�^�^�F��!�� � �4��0��0�0�0�0�0�0�0�0�F� �I�I�c�6�3� '� '� '��Mrr)rhrmr&s` �r� decoratorzcached.<locals>.decorator�s9���� �t��� � � � � � �� � �r)rhr r)r&rns` r�cachedro�s)���������( �r)rXrrYrrrHrW)r&rL)r� __future__r�loggingr(r6� dataclassesr� functoolsr�typingrrr r r r r� getLoggerrr8rrrr#rfrorrr�<module>rvsw�� � �#�"�"�"�"�"��������� � � � �!�!�!�!�!�!�������B�B�B�B�B�B�B�B�B�B�B�B�B�B�B�B������� �� �8� $� $�� �G�C�L�L�� ����������� ���`*�`*�`*�`*�`*�`*�`*�`*�H �����1�1�1�1�!�!�!�!�!�!�!r

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/amanasmuei/mcp-server-malaysia-prayer-time'

If you have feedback or need assistance with the MCP directory API, please join our Discord server