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

External cache-key references on cacheable decorator #178

Closed
cabol opened this issue Nov 20, 2022 · 0 comments
Closed

External cache-key references on cacheable decorator #178

cabol opened this issue Nov 20, 2022 · 0 comments

Comments

@cabol
Copy link
Owner

cabol commented Nov 20, 2022

Previously in #169 the support for key references on the same was implemented, like "internal references". Despite this could be the common case scenario, there could be situations where you may want to reference a key that is stored in a different and/or external cache. But let's give a possible use case. Assume we have a Redis cache (using the NebulexRedisAdapter), and you want to optimize the calls to Redis as much as possible. Therefore, you would like to store the referenced keys in a local cache and the actual values in Redis. This way, we only hit Redis for accessing the keys with the actual values, and the referenced keys will be resolved locally.

This is an example of the proposed solution:

defmodule MyApp.UserAccounts do
  use Nebulex.Caching

  alias MyApp.{LocalCache, RedisCache}

  @decorate cacheable(cache: RedisCache, key: id)
  def get_user_account(id) do
    # your logic ...
  end

  @decorate cacheable(
              cache: LocalCache,
              key: email,
              references: &cache_ref(RedisCache, &1.id)
            )
  def get_user_account_by_email(email) do
    # your logic ...
  end

  @decorate cacheable(
              cache: LocalCache,
              key: token,
              references: &cache_ref(RedisCache, &1.id)
            )
  def get_user_account_by_token(token) do
    # your logic ...
  end

  @decorate cache_evict(cache: RedisCache, key: user.id)
  def update_user_account(user) do
    # your logic ...
  end
end

The functions get_user_account/1 and update_user_account/2 use RedisCache to store the real value in Redis while get_user_account_by_email/1 and get_user_account_by_token/1 use LocalCache to store the referenced keys. Then, with the option references: &cache_ref(RedisCache, &1.id) we are telling the cacheable decorator the referenced key given by &1.id is located in the cache RedisCache; underneath, the macro cache_ref/2 builds the special return type for the external cache reference.

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

No branches or pull requests

1 participant