Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
"""LICENSE Copyright 2018 Hermann Krumrey <hermann@krumreyh.com>
This file is part of anime-list-apis.
anime-list-apis is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
anime-list-apis is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with anime-list-apis. If not, see <http://www.gnu.org/licenses/>. LICENSE"""
""" Handles various caching functionality """
CacheModelType.DATA: None, CacheModelType.MEDIA_DATA: MediaData, CacheModelType.MEDIA_USER_DATA: MediaUserData, CacheModelType.MEDIA_LIST_ENTRY: MediaListEntry } """ Maps model types to their respective classes """
self, cache_location: str = None, expiration: int = 6000, write_after: int = 20 ): """ Initializes the Cache. If the Cache directory and file do not exist, they will be created here. :param cache_location: The location of the cache. Will default to a hidden directory in the user's home directory :param expiration: Defines how long objects should be valid. If set to a negative number, will be infinite :param write_after: Defines after how many cache changes the changes are automatically written to the cache file """
if cache_location is None: # pragma: no cover self.cache_location = os.path.join( os.path.expanduser("~"), ".anime_list_apis" ) else:
""" Writes the content of the cache to the cache file :return: None """
self.__cache[model_type][site_type]
else:
"timestamp": self.__cache[model_type][site_type][tag] ["timestamp"], "data": self.__cache[model_type][site_type][tag] ["data"].serialize() }
serialized, f, sort_keys=True, indent=4, separators=(",", ": ") )
""" Loads the content of the cache file into memory :return: None """
else: "timestamp": entry["timestamp"], "data": data_class.deserialize(entry["data"]) }
""" Adds a primitive data object to the cache :param site_type: The site for which to cache :param key: The key of the value :param value: the value to cache :return: None """ "timestamp": time.time(), "value": value }
self, site_type: IdType, data: CacheAble, ignore_for_write_count: bool = False ): """ Adds a copy of an object to the cache. If the amount of changes exceeds the amount defined in write_after, write to file afterwards :param site_type: The site for which to cache it :param data: The data to cache :param ignore_for_write_count: If set to True, will not increment the change_count variable. :return: None """
else: else:
"timestamp": time.time(), "data": deepcopy(data) }
""" Retrieves a primitive data object from the cache :param site_type: The site for which to get the cached data :param key: The key to retrieve :return: The cached primitive data object or None if no entry in cache """
else: else:
self, model_type: CacheModelType, site_type: IdType, media_type: MediaType, _id: int or Id, username: Optional[str] = None ) -> Optional[CacheAble]: """ Retrieves a cached object. If the object has expired, remove it from the cache :param model_type: The cached data type :param site_type: The site type for which the object was cached :param media_type: The media type of the object to get :param _id: The ID to search for :param username: Optional-The username associated with the data object :return: A copy of the cached object, or None if it wasn't found """ CacheModelType.MEDIA_DATA, site_type, media_type, _id ) CacheModelType.MEDIA_USER_DATA, site_type, media_type, _id, username )
else:
else: else:
self, site_type: IdType, media_type: MediaType, _id: int or Id ) -> Optional[MediaData]: """ Retrieves a media data entry from the cache :param site_type: The site for which to fetch an entry :param media_type: The type of the media :param _id: The ID to fetch :return: The entry data or None if no corresponding entry exists """ CacheModelType.MEDIA_DATA, site_type, media_type, _id )
self, site_type: IdType, media_type: MediaType, _id: int or Id, username: str ) -> Optional[MediaUserData]: """ Retrieves a media user entry from the cache :param site_type: The site type for which the object was cached :param media_type: The media type of the cached object :param _id: The ID of the corresponding media data :param username: The username of the entry :return: The entry or None if not found """ CacheModelType.MEDIA_USER_DATA, site_type, media_type, _id, username )
self, site_type: IdType, media_type: MediaType, _id: int or Id, username: str ) -> Optional[MediaListEntry]: """ Retrieves a media list entry from the cache :param site_type: The site for which to fetch the cached object :param media_type: The media type of the entry :param _id: The ID of the entry :param username: The username of the entry :return: The entry or None if not found """ CacheModelType.MEDIA_LIST_ENTRY, site_type, media_type, _id, username )
self, model_type: CacheModelType, site_type: IdType, media_type: MediaType, _id: int or Id, username: Optional[str] = None ): """ Invalidates a cache entry :param model_type: The model type of the entry to invalidate :param site_type: The site type of the entry to invalidate :param media_type: The media type of the entry :param _id: The ID of the entry :param username: The username for which to invalidate the entry :return: None """ CacheModelType.MEDIA_DATA, site_type, media_type, _id ) CacheModelType.MEDIA_USER_DATA, site_type, media_type, _id, username ) else:
# Write to make sure that cache entry is no longer accessible
self, site_type: IdType, media_type: MediaType, _id: int or Id ): """ Invalidates a media entry :param site_type: The site type of the entry :param media_type: The media type of the entry :param _id: The ID of the entry :return: None """
self, site_type: IdType, media_type: MediaType, _id: int or Id, username: str ): """ Invalidates a user data entry :param site_type: The site type of the entry :param media_type: The media type of the entry :param _id: The ID of the entry :param username: The user for which to invalidate the entry :return: None """ CacheModelType.MEDIA_USER_DATA, site_type, media_type, _id, username )
self, site_type: IdType, media_type: MediaType, _id: int or Id, username: str ): """ Invalidates a list entry :param site_type: The site type of the entry :param media_type: The media type of the entry :param _id: The ID of the entry :param username: The user for which to invalidate the entry :return: None """ CacheModelType.MEDIA_LIST_ENTRY, site_type, media_type, _id, username )
-> Dict[ CacheModelType, Dict[ IdType, Dict[ str, Dict[ str, int or Serializable ] ] ] ]: """ Generates a fresh cache. The cache has the following structure:
{ CacheModelType: { SiteType: { MediaType + Id: { "timestamp": float, "data": data } } } }
The CacheModelType 'MediaListEntry' will be split into their 'MediaData' and 'MediaUserData' components, so there will be no separate entry for them. Additionally, a 'data' entry will be added for caching various primitive data :return: The generated cache dictionary """
""" Turns an ID into an int if it is not already :param site_type: The type of site for which to store the ID :param _id: The ID to convert :return: The int ID """ else:
media_type: MediaType, _id: int, username: Optional[str] = None ) -> str: """ Generates an ID tag for the cache :param media_type: The media type of the tag :param _id: The ID of the entry :param username: Optionally create a tag for a specific username :return: The generated ID tag """ |