Skip to content

Commit 2fd2587

Browse files
committed
Cleanup and document Client.set_cache
- Allow for multiple cache instances to be passed and changed in 1 call - Add documentation and a note about how calling this will override existing cache instance
1 parent 0a2b7dd commit 2fd2587

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

coc/client.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,27 @@ def dispatch(self, event_name, *args, **kwargs):
207207
else:
208208
event(*args, **kwargs)
209209

210-
def set_cache(self, cache_to_edit, max_size=128, ttl=None):
211-
cache_type = getattr(self, cache_to_edit)
212-
if not cache_type:
213-
raise ValueError('{} is not a cached data class'.format(cache_to_edit))
210+
def set_cache(self, *cache_to_edit, max_size=128, ttl=None):
211+
"""Set the max size and expiry time for a cached object.
214212
215-
cache = Cache(max_size=max_size, ttl=ttl)
216-
setattr(self, cache_to_edit, cache)
213+
.. note::
214+
215+
Calling this method will override and create a new cache instance,
216+
removing all previously cached objects
217+
218+
219+
:param cache_to_edit: :class:`str` or :class:`CacheType` enum. The name of cache type to change.
220+
:param max_size: :class:`int` The max size of the created cache. Defaults to 128
221+
:param ttl: :class:`int` The expiry time in seconds of the cache. Defaults to None (cache does not expire)
222+
"""
223+
for cache_type in cache_to_edit:
224+
if not getattr(self, cache_type):
225+
raise ValueError('{} is not a valid cached data class type'.format(cache_to_edit))
226+
227+
cache = Cache(max_size=max_size, ttl=ttl)
228+
log.debug('Cache type %s has been set with max size %s and expiry %s seconds',
229+
cache_type, max_size, ttl)
230+
setattr(self, cache_type, cache)
217231

218232
async def search_clans(self, *, name: str=None, war_frequency: str=None,
219233
location_id: int = None, min_members: int=None, max_members: int=None,

0 commit comments

Comments
 (0)