cronokirby / alchemy

A discord library for Elixir
MIT License
152 stars 34 forks source link

[refactor] Restructuring cache #84

Open curz46 opened 4 years ago

curz46 commented 4 years ago

Motivation

The data stored by the cache is the parsed JSON, rather than the structs the user deals with.

Proposed Structure

(partially through discussion with @cronokirby and others, partially my own thoughts)

Notes

OvermindDL1 commented 4 years ago

ETS doesn't actually implement locking, however mnesia does, so this actually seems like a case for mnesia instead. In addition, if it's needed (not for my bot), the mnesia database can be replicated onto multiple nodes. mnesia just wraps ETS (and DETS too if you set it to write anything to disk, though not in this case) so you get all of ETS features, in addition to a few others like locking, transactions, etc...

cronokirby commented 4 years ago

We should aim to normalize things, to avoid storing redundant structs in the cache, and to make accessing individual structs easy. That is to say, we should strip complex objects like Guilds from the objects they contain, and instead just leave their snowflake / id. Each object can have its own ets / mnesia table.

As far as locking / transactions goes, we need to be able to make sure that when handling an event which requires inserting multiple things, we do so in an atomic way, to avoid running into interleaving issues like we had previously.

OvermindDL1 commented 4 years ago

As far as locking / transactions goes, we need to be able to make sure that when handling an event which requires inserting multiple things, we do so in an atomic way, to avoid running into interleaving issues like we had previously.

Definitely mnesia over ETS.

Actually at this point I'm leaning to using Cachex, it has transaction support, distributed caches, etc...

cronokirby commented 4 years ago

I've created a Milestone / Project to track issues related to this effort: https://github.com/cronokirby/alchemy/projects

I don't expect this to come as one PR, so I've created the new-cache branch. As we slowly work on this branch to replace the cache, let's create issues tagged with the new-cache label, and keep track of them under the project board.

I think Cachex should be used, as it provides a very solid implementation of a Cache, and lets us focus on the discord specific logic. It also has great support for transactions and grouping queries, which will be very useful for us.

The first things to do at this point would be to split up the Cache work into small issues. A good place to start would be replacing one of the smaller ets tables or processes using Cachex, and adding a good test suite. This would serve as an example for how to do the rest of the cache. Replacing the guild cache is probably the last thing to do, as it relies on the rest of the cache working well.

cronokirby commented 4 years ago

Oh, and I've labelled this issue as discussion. Let's keep this issue as a centralized place to track work on this project :)