Cache
General Cache Flow¶
flowchart TD
request <--> load
request <-->|if delete request|delete <--> find
request <--> |if get or put request|add <--> find
request <---> |if patch or post request|update <--> find
request <---> |if cache is valid \n and get request| find
request[request\n method] --> valid[\valid?/] --> save
valid ----> returnn
save -----> returnl[return list of objects]
save -----> returnn[return none]
save -----> returno[return single object]
toggl_api.modules.meta.cache.TogglCache
¶
Bases: ABC
Abstract class for caching toggl API data to disk.
Integrates fully with TogglCachedEndpoint to create a seemless depending on the users choice of cache.
Methods:
-
commit
–Commits the cache to disk, database or other form. Basically method for finalising the cache. Abstract.
-
load_cache
–Loads the cache from disk, database or other form. Abstract.
-
save_cache
–Saves and preforms action depending on request type. Abstract.
-
find_entry
–Looks for a TogglClass in the cache. Abstract.
-
add_entry
–Adds a TogglClass to the cache. Abstract.
-
update_entry
–Updates a TogglClass in the cache. Abstract.
-
delete_entry
–Deletes a TogglClass from the cache. Abstract.
-
find_method
–Matches a RequestMethod to cache functionality.
-
parent_exist
–Validates if the parent has been set. The parent will be generally set by the endpoint when assigned. Abstract.
-
query
–Queries the cache for various varibles. Abstract.
Attributes:
-
_cache_path
–Path to the cache file. Will generate the folder if it does not exist.
-
_expire_after
–Time after which the cache should be refreshed. If using an integer it will be assumed as seconds. If set to None its ignored.
-
_parent
–Parent TogglCachedEndpoint
toggl_api.modules.meta.cache.SqliteCache
¶
Bases: TogglCache
Class for caching data to a Sqlite database.
Parameters:
-
expire_after
(Optional[timedelta | int]
, default:None
) –Time after which the cache should be refreshed. If using an integer it will be assumed as seconds. If set to None the cache will never expire.
-
parent
(Optional[TogglCachedEndpoint]
, default:None
) –Parent endpoint that will use the cache. Usually assigned automatically when supplied to a cached endpoint.
Attributes:
-
database
–Sqlalchemy database engine.
-
metadata
–Sqlalchemy metadata.
-
session
–Sqlalchemy session.
Methods:
-
load_cache
–Loads the data from disk and stores it in the data attribute. Invalidates any entries older than expire argument.
toggl_api.modules.meta.cache.json_cache.JSONSession
dataclass
¶
Data structure for storing JSON in memory.
Similar to a SQL session as its meant to have the same/similar interface.
Methods:
-
save
–Saves the data to a JSON file. Setting current timestamp and version.
-
load
–Loads the data from disk and stores it in the data attribute. Invalidates any entries older than expire argument.
Attributes:
-
max_length
(int
) –Max length of the data to be stored.
-
version
(str
) –Version of the data structure.
-
data
(list[TogglClass]
) –List of Toggl objects stored in memory.
-
modified
(int
) –Timestamp of when the cache was last modified in nanoseconds. Used for checking if another cache object has updated it recently.
toggl_api.modules.meta.cache.JSONCache
¶
Bases: TogglCache
Class for caching Toggl data to disk in JSON format.
Parameters:
-
path
(Path
) –Path to the cache file
-
expire_after
(Optional[timedelta | int]
, default:None
) –Time after which the cache should be refreshed. If using an integer it will be assumed as seconds. If set to None the cache will never expire.
-
parent
(Optional[TogglCachedEndpoint]
, default:None
) –Parent endpoint that will use the cache. Usually assigned automatically when supplied to a cached endpoint.
-
max_length
(int
, default:10000
) –Max length of the data to be stored.
Methods:
-
commit
–Wrapper for JSONSession.save() that saves the current json data to disk.
-
save_cache
–Saves the given data to the cache. Takes a list of Toggl objects or a single Toggl object as an argument and process the change before saving.
-
load_cache
–Loads the data from the cache and returns the data to the caller discarding expired entries.
Attributes:
-
session(JSONSession)
–Store the current json data in memory while handling the cache.