smiley/steamapi

How exactly do I expire a property's cache?

Opened this issue · 4 comments

I'm trying to check the state property of a user and I believe i'm getting the cached version of the property. I read in your documentation "to expire a cached property value manually just do::
del instance._cache[property name]", however I seem to not be able to refresh it. Sorry, i'm new to programming and don't really understand this concept.

Each SteamObject has a "private" property called _cache. It's a dictionary mapping between the property name, and the (1) data of it & (2) the time it was retrieved.

Properties can be (optionally) cached: either indefinitely (once retrieved -- never check again) or for a period of time. Caching is done on a per-object level -- not a global one -- so times are adjusted based on assumptions made regarding what you'll do with that object. For example, SteamUser.country_code is cached indefinitely (steamapi/user.py:203) because it's unlikely that a user will change it for as long as you have the object.

When you ask for a cached property (e.g.: smiley.friends) the object first looks at its cache and checks if it has a value for that name (friends). If it does, it looks at the time it was retrieved and compares it to now. If the difference is bigger than what the property's cache timeout is (e.g.: 1 hour), the code in charge of retrieving the relevant data is called again, and the cache is replaced:

>>> smiley = user.SteamUser(userurl=smileybarry)
>>> smiley.currently_playing
<SteamApp "Left 4 Dead 2" (550)>
>>> game = _
>>> smiley._cache['currently_playing']
(<SteamApp "Left 4 Dead 2" (550)>, 2948329593.0) # The second number is a placeholder for the time (number of seconds since 1970) it was retrieved
>>> id(game) == smiley._cache['currently_playing'][0]
True # Same object
>>> del smiley._cache['currently_playing']
>>> new_game = smiley.currently_playing
>>> new_game
<SteamApp "Left 4 Dead 2" (550)> # Semantically, this is the same game -- but it's not the same object.
>>> id(game) == id(new_game)
False # New object, not the same one

Caching was introduced because Valve's API server is pretty slow. It's US-bound, it takes some time for the simplest things, and originally it dropped requests or delayed them by seconds(!). Querying it for everything would be terrible, and I didn't want users/developers to side-step this issue themselves by using temporary objects.

This has clarified many things for me. Thank you so much! I really should have read the wiki before posting this issue first but you'll have to forgive me since I am still new to Github. So if I only want to refresh the state property, I will have to delete the _summary cache first? As of right now, I have del friendToTrack._cache["_summary"] Is this the correct way of updating the state property?

Yep, that's the one. That will delete the saved data for _summary. The next time you call friendToTrack.state, _summary will be retrieved from the server. (Basically, not when you del it, but the next time you ask for it after del-ing)

Alright cool! Thank you so much. I appreciate all the assistance you gave me. I got it working now. You did a great job of making this library beginner-friendly. Should I close this issue or leave it up? Will other's be able to see this issue after it is closed?