add storage functions, use phonenumbers
Hey,
because the current implementation of storage.read() or storage.save() do not work well when they run simultaneously i added the storage.smembers() and storage.sadd() capabilities from redis to the RedisStorage and InMemoryStorage classes.
the later is untested, though.
smembers - list the set of stored data
sadd - add item to the end of the existing data
i have a command that fetches content from an website. this takes time. to make sure a user never receives the same post twice i used to:
- storage.read()
- do stuff
- storage_result.append(new_id)
- storage.save()
if the user runs the same command twice in a row it might happen that both coroutines read the same data and only the last one is actually saved.
now smembers and sadd will handle the "append" and this eliminates the race condition.
i also quickly implemented phonenumbers (#29)
EDIT: typo
oh.
and storage.delete() is now a thing.
squashed
smembers() does now return a list instead of a set.
i think this will be more generic.
i donno if this is an antipattern or okay to do it this way.
WDYT?
i decided that the set should be returned as such. :)
Thank you for the PR! Always nice to see contributions from others :)
I understand the need for smembers and sadd in your case but I don't like that the implementation kind of forces other storage technologies to implement those methods now as well. The motivation behind bot.storage is to provide a quick and easy way for the user to store stuff without worrying too much about file handlers, connections, etc. Anything more complicated should be hidden. But it is kind of limited I admit.
What do you think about the following approach: The storage class also provides the public attributes bot.storage.redis and bot.storage.inmemory (as well as any other technology in the future?) so anything more "complicated" can be accessed that way. Depending on the bot configuration, bot.storage.redis will be either None or the Redis instance.
storage.delete() does sound useful, I'd add it to the abstract storage definition
Hey,
Yeah, i was not sure if this would fit the style. Your approach might be more feasible and would provide the necessary functionality anyway.
i'll update this PR to accommodate this.
this works like this:
seen_posts = c.bot.storage.redis.smembers(storage_key)
c.bot.storage.redis.sadd(storage_key, item["id"])
WDYT?
i have rebased the codebase and updated the deps. :)
see #60