signalbot icon indicating copy to clipboard operation
signalbot copied to clipboard

add storage functions, use phonenumbers

Open Kariton opened this issue 2 years ago • 9 comments

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

Kariton avatar Sep 08 '23 21:09 Kariton

oh. and storage.delete() is now a thing.

Kariton avatar Sep 08 '23 21:09 Kariton

squashed

Kariton avatar Sep 09 '23 16:09 Kariton

smembers() does now return a list instead of a set.

i think this will be more generic.

Kariton avatar Sep 09 '23 16:09 Kariton

i donno if this is an antipattern or okay to do it this way.

WDYT?

Kariton avatar Sep 09 '23 16:09 Kariton

i decided that the set should be returned as such. :)

Kariton avatar Sep 09 '23 21:09 Kariton

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

filipre avatar Sep 20 '23 20:09 filipre

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.

Kariton avatar Sep 25 '23 22:09 Kariton

this works like this:

seen_posts = c.bot.storage.redis.smembers(storage_key)

c.bot.storage.redis.sadd(storage_key, item["id"])

WDYT?

Kariton avatar Oct 22 '23 22:10 Kariton

i have rebased the codebase and updated the deps. :)

Kariton avatar Jan 20 '24 10:01 Kariton

see #60

Kariton avatar Jun 02 '24 16:06 Kariton