Packages
unleash_fresha
5.1.2
5.1.4
5.1.3
5.1.2
5.1.1
5.1.0
5.0.0
4.0.0
4.0.0-git-bda3
3.0.1
3.0.0
2.1.0
2.0.1
2.0.0-git-75d8
2.0.0-git-67b2
2.0.0-git-4b61
2.0.0-git-49f4
1.16.0
1.15.0
1.14.1-git-e754
1.14.0
1.14.0-git-71e9
1.13.0
1.13.0-git-4ba6
1.12.0
1.11.0
1.11.0-git-1b62
1.10.2
1.10.2-git-b835
1.10.1
1.10.0
1.9.3-alpha0
1.9.2
1.9.1
1.9.0
An Unleash Feature Flag client for Elixir, forked from [unleash](https://gitlab.com/afontaine/unleash_ex)
Current section
Files
Jump to
Current section
Files
lib/unleash/cache.ex
defmodule Unleash.Cache do
@moduledoc """
This module is a cache backed by an ETS table. We use it to allow for multiple
threads to read the feature flag values concurrently on top of minimizing
network calls
"""
alias Unleash.Config
@cache_table_name :unleash_cache
@doc false
def cache_table_name, do: @cache_table_name
@doc """
Will create a new ETS table named `:unleash_cache`
"""
def init(existing_features \\ [], table_name \\ @cache_table_name) do
:ets.new(table_name, [:named_table, read_concurrency: true])
upsert_features(existing_features, table_name)
end
@doc """
Will return all values currently stored in the cache
"""
def get_all_feature_names(table_name \\ @cache_table_name) do
features = :ets.tab2list(table_name)
Enum.map(features, fn {_name, feature} ->
feature.name
end)
end
@doc """
Will return all features stored in the cache
"""
def get_features(table_name \\ @cache_table_name) do
features = :ets.tab2list(table_name)
Enum.map(features, fn {_name, feature} ->
feature
end)
end
@doc """
Will return the feature for the given name stored in the cache
"""
def get_feature(name, table_name \\ @cache_table_name) do
key = to_key(name)
case :ets.lookup(table_name, key) do
[{^key, feature}] -> feature
[] -> nil
end
end
@doc """
Will upsert (create or update) the given features in the cache
This will clear the existing peristed features to prevent stale reads
When `Unleash.Config.allowed_features/0` is set, only features whose name is in
that list are stored, keeping the cache minimal for services that use just a
few flags. When it is `nil` (the default), every feature is cached.
"""
def upsert_features(features, table_name \\ @cache_table_name) do
features =
features
|> filter_allowed(Config.allowed_features())
|> Enum.map(&prepare_feature(&1.name, &1))
:ets.delete_all_objects(table_name)
:ets.insert(table_name, features)
end
defp filter_allowed(features, nil), do: features
defp filter_allowed(features, allowed) do
allowed_names = MapSet.new(allowed, &normalize/1)
keep = with_dependency_parents(features, allowed_names)
Enum.filter(features, fn feature -> MapSet.member?(keep, normalize(feature.name)) end)
end
# Features that an allowed feature depends on must also be cached, otherwise
# the dependency cannot be resolved and the child would always read as
# disabled. We add the direct parents of every allowed feature to the keep
# set. A single level is sufficient because a parent that itself depends on
# another feature already disables its child (see `Unleash.Runtime`).
defp with_dependency_parents(features, allowed_names) do
parent_names =
features
|> Enum.filter(fn feature -> MapSet.member?(allowed_names, normalize(feature.name)) end)
|> Enum.flat_map(&dependency_keys/1)
|> MapSet.new()
MapSet.union(allowed_names, parent_names)
end
defp dependency_keys(%{dependencies: deps}) when is_list(deps) do
Enum.map(deps, fn dep -> normalize(dep["feature"]) end)
end
defp dependency_keys(_feature), do: []
# Case-insensitive string form of a feature name, used only to test allowlist
# membership. Comparing on strings avoids interning every server-sent name as
# an atom: only the features we actually keep are turned into atom keys (see
# `to_key/1`), so the atom table grows with the cached flags, not the feed.
defp normalize(name) when is_binary(name), do: String.downcase(name)
defp normalize(name) when is_atom(name), do: Atom.to_string(name)
defp prepare_feature(name, value) do
{to_key(name), value}
end
# Feature names are stored as lowercase atoms. Binary names (e.g. from the
# Unleash API) are downcased and atomized so storage is case-insensitive. Atoms
# are assumed to already be lowercase and are used directly as the key, so the
# `enabled?(:my_feature)` lookup path does zero conversion. Feature names are
# bounded by the number of flags, so dynamic atom creation is safe here.
defp to_key(name) when is_binary(name), do: name |> String.downcase() |> String.to_atom()
defp to_key(name) when is_atom(name), do: name
end