Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easy way to declare custom Cache #973

Open
1 task done
CircuitSacul opened this issue Jan 21, 2022 · 11 comments
Open
1 task done

Easy way to declare custom Cache #973

CircuitSacul opened this issue Jan 21, 2022 · 11 comments
Labels
enhancement New feature or request

Comments

@CircuitSacul
Copy link
Contributor

Summary

It would be nice if I could declare a cache class in CacheSettings. Currently you have to patch multiple things, including Bot._cache and Bot._event_manager._cache.

Why is this needed?

To add to or modify the default cache.

Ideal implementation

An extra setting in CacheSettings for cache class; it should expect a subclass of CacheImpl. Then it can set self._cache = cache_settings.cache_class(...), or something of the sort.

Checklist

  • I have searched the issue tracker and have made sure it's not a duplicate. If it is a follow up of another issue, I have specified it.
@CircuitSacul CircuitSacul added the enhancement New feature or request label Jan 21, 2022
@ahnaf-zamil
Copy link
Contributor

The CacheImpl class itself takes a CacheSettings object in the constructor, so that would be impractical to declare a Cache class in the settings. The GatewayBot class has a property called _cache which is the cache implementation that the bot uses.

hikari/hikari/impl/bot.py

Lines 342 to 344 in 965d6e8

# Caching
cache_settings = cache_settings if cache_settings is not None else config.CacheSettings()
self._cache = cache_impl.CacheImpl(self, cache_settings)

You can probably subclass it and override it to use your own cache implementation. If you want to implement your own cache, consider implementing this abstract class

class Cache(abc.ABC):

From what I see, there is no way to add a Cache class to cache settings since the Cache object itself takes in the settings when it's being constructed. All I can tell you is to subclass GatewayBot, set the _cache property to your own cache implementation, then use it.

But I think it would be better for the GatewayBot class to take in a cache implementation as a constructor arg, instead of taking the cache impl in the settings. I will leave that decision to Dav and others since I don't maintain this project :)

@CircuitSacul
Copy link
Contributor Author

I see. The problem is you have to patch two places; Bot._cache and Bot._event_manager._cache. Maybe an extra option on Bot that accepts a cache class would be best.

@ahnaf-zamil
Copy link
Contributor

Yeah that's what I said in the last part of my reply. The GatewayBot could take in a cache impl as a constructor arg, and then use that. But at the moment, it's hardcoded to use hikari's default CacheImpl, and the only way to change it is to override _cache and _event_manager._cache.

@ahnaf-zamil
Copy link
Contributor

I just realized, you don't need to override _event_manager._cache since the event manager takes in GatewayBot._cache. The RESTClient does so as well. So all that needs to be done is to add a feature to set custom cache class, if I'm not being dumb rn

@FasterSpeeding
Copy link
Collaborator

Why is this needed?
To add to or modify the default cache.

What's the actual use case for this though

@ahnaf-zamil
Copy link
Contributor

Why is this needed?
To add to or modify the default cache.

What's the actual use case for this though

Maybe someone might want to use their own cache implementation. For example, I personally had thought of the idea to use Redis for cache. Even though not everyone will use their custom cache impl, having a pluggable cache seems like a nice feature

@FasterSpeeding
Copy link
Collaborator

Why is this needed?
To add to or modify the default cache.

What's the actual use case for this though

Maybe someone might want to use their own cache implementation. For example, I personally had thought of the idea to use Redis for cache. Even though not everyone will use their custom cache impl, having a pluggable cache seems like a nice feature

This isn't a viable way to add an asynchronous cache, you'd have to have that as a separate thing

@ahnaf-zamil
Copy link
Contributor

Why is this needed?
To add to or modify the default cache.

What's the actual use case for this though

Maybe someone might want to use their own cache implementation. For example, I personally had thought of the idea to use Redis for cache. Even though not everyone will use their custom cache impl, having a pluggable cache seems like a nice feature

This isn't a viable way to add an asynchronous cache, you'd have to have that as a separate thing

Separate thing hmmm... Like what exactly?

@CircuitSacul
Copy link
Contributor Author

Why is this needed?
To add to or modify the default cache.

What's the actual use case for this though

Adding/modifying an existing cache. For example, the current cache only stores existing values. What if I wanted to store None for deleted messages, so that I don't fetch non-existing messages repeatedly?

An async cache would have to be seperate, but here's a custom cache I use right now: https://github.com/TrigonDev/Starboard/blob/main/starboard/cache.py

@FasterSpeeding
Copy link
Collaborator

FasterSpeeding commented Jan 24, 2022

For example, the current cache only stores existing values. What if I wanted to store None for deleted messages, so that I don't fetch non-existing messages repeatedly?

BTW this example would likely fall under #880 (although it'd never quite apply to messages since that cache store is always incomplete)

@Lunarmagpie
Copy link
Contributor

This isn't a viable way to add an asynchronous cache, you'd have to have that as a separate thing

I think there is with a library like greenback. Obviously it's a performance drop but I would take that because it will allow me to use speedups from libraries that can skip API calls if they can interact with the cache. (Both crescent and flare have this.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants