ExOwm v1.0.0-rc.1 ExOwm.Cache.SixteenDayForecast View Source

Link to this section Summary

Functions

Returns the adapter configuration stored in the :otp_app environment

Deletes the cached entry in for a specific key

Flushes the cache

Fetches a value or object from Cache where the key matches the given key

Similar to get/2 but raises KeyError if no value associated with the given key was found

Gets the value from key and updates it, all in one pass

Returns whether the given key exists in Cache

Returns true if the current process is inside a transaction

Returns all cached keys

Returns and removes the value/object associated with key in Cache

Returns a list of hook functions that will be executed after invoke the cache action

Returns a list of hook functions that will be executed before invoke the cache action

Invokes reducer for each entry in the cache, passing the key, the return and the accumulator acc as arguments. reducer’s return value is stored in acc

Sets the given value under key in the Cache

Returns the cache size (total number of cached entries)

Starts a supervision and return {:ok, pid} or just :ok if nothing needs to be done

Shuts down the cache represented by the given pid

Returns a map with all cache entries (key/value). If you want the map values be the cache object, pass the option :return set to :object

Runs the given function inside a transaction

Updates the cached key with the given function

Updates (increment or decrement) the counter mapped to the given key

Link to this section Functions

Returns the adapter configuration stored in the :otp_app environment.

If the c:init/2 callback is implemented in the cache, it will be invoked.

Callback implementation for Nebulex.Cache.config/0.

Deletes the cached entry in for a specific key.

It always returns the key, either it exists or not. It the key exists, it’s removed from cache, otherwise nothing happens.

Options

  • :on_conflict - It may be one of :raise (the default), :nothing, :delete. See the “OnConflict” section for more information.

Note that for this function :return option hasn’t any effect since it always returns the key either success or not.

See the “Shared options” section at the module documentation.

Example

nil =
  :a
  |> MyCache.set(1, return: :key)
  |> MyCache.delete()
  |> MyCache.get()

:non_existent_key = MyCache.delete :non_existent_key

OnConflict

The :on_conflict option supports the following values:

  • :raise - raises if there is a conflicting key
  • :nothing - ignores the error in case of conflicts
  • :delete - deletes the value on the existing key

Examples

# Set a value
1 = MyCache.set :a, 1

# Delete with an invalid version but do nothing on conflicts.
# Keep in mind that, although this returns successfully, the returned
# `kwy` does not reflect the data in the Cache. For instance, in case
# of "on_conflict: :nothing", the returned `key` isn't deleted.
:a = MyCache.delete :a, version: :invalid, on_conflict: :nothing
1 = MyCache.get :a

# Delete with the same invalid version but force to delete the current
# value on conflicts (if it exist).
:a = MyCache.delete :a, version: :invalid, on_conflict: :delete
nil = MyCache.get :a

Callback implementation for Nebulex.Cache.delete/2.

Flushes the cache.

Examples

for x <- 1..5, do: MyCache.set(x, x)

:ok = MyCache.flush

for x <- 1..5, do: nil = MyCache.get(x)

Callback implementation for Nebulex.Cache.flush/0.

Fetches a value or object from Cache where the key matches the given key.

Returns nil if no result was found.

Options

  • :on_conflict - It may be one of :raise (the default), :nothing, nil. See the “OnConflict” section for more information.

See the “Shared options” section at the module documentation.

Example

"some value" =
  :a
  |> MyCache.set("some value", return: :key)
  |> MyCache.get

nil = MyCache.get :non_existent_key

OnConflict

The :on_conflict option supports the following values:

  • :raise - raises if there is a conflicting key
  • :nothing - ignores the error in case of conflicts
  • nil - forces to return nil

Examples

# Set a value
1 = MyCache.set :a, 1

# Gets with an invalid version but do nothing on conflicts.
# Keep in mind that, although this returns successfully, the returned
# struct does not reflect the data in the Cache. For instance, in case
# of "on_conflict: :nothing", it returns the latest version of the
# cached object.
%Nebulex.Object{key: :a, value: 1} =
  MyCache.get :a, return: :object, version: :invalid, on_conflict: :nothing
1 = MyCache.get :a

# Gets with the same invalid version but force to return `nil`
nil = MyCache.get :a, version: :invalid, on_conflict: nil
1 = MyCache.get :a

Callback implementation for Nebulex.Cache.get/2.

Similar to get/2 but raises KeyError if no value associated with the given key was found.

Options

See the “Shared options” section at the module documentation.

Example

MyCache.get!(:a)

Callback implementation for Nebulex.Cache.get!/2.

Link to this function get_and_update(key, fun, opts \\ []) View Source

Gets the value from key and updates it, all in one pass.

fun is called with the current cached value under key (or nil if key hasn’t been cached) and must return a two-element tuple: the “get” value (the retrieved value, which can be operated on before being returned) and the new value to be stored under key. fun may also return :pop, which means the current value shall be removed from Cache and returned.

The returned value is a tuple with the “get” value returned by fun and the new updated value under key.

Options

  • :on_conflict - same as callback get/2.

Note that for this function :return option hasn’t any effect, since it always returns a tuple {get :: value, update :: value} in case of success.

See the “Shared options” section at the module documentation.

Examples

# update a nonexistent key
{nil, "value!"} = MyCache.get_and_update(:a, fn current_value ->
  {current_value, "value!"}
end)

# update a existent key
{"value!", "new value!"} = MyCache.get_and_update(:a, fn current_value ->
  {current_value, "new value!"}
end)

# pop/remove value if it exists
{"new value!", nil} = MyCache.get_and_update(:a, fn _ -> :pop end)

# pop/remove a nonexistent key
{nil, nil} = MyCache.get_and_update(:b, fn _ -> :pop end)

Callback implementation for Nebulex.Cache.get_and_update/3.

Returns whether the given key exists in Cache.

Examples

1 = MyCache.set(:a, 1)

true = MyCache.has_key?(:a)

false = MyCache.has_key?(:b)

Callback implementation for Nebulex.Cache.has_key?/1.

Returns true if the current process is inside a transaction.

Examples

MyCache.in_transaction?
#=> false

MyCache.transaction(fn ->
  MyCache.in_transaction? #=> true
end)

Callback implementation for Nebulex.Cache.in_transaction?/0.

Returns all cached keys.

Examples

for x <- 1..3, do: MyCache.set(x, x*2, return: :key)

[1, 2, 3] = MyCache.keys

WARNING: This is an expensive operation, beware of using it in prod.

Callback implementation for Nebulex.Cache.keys/0.

Link to this function new_generation(opts \\ []) View Source

Returns and removes the value/object associated with key in Cache.

Returns nil if no result was found.

Options

  • :on_conflict - same as callback get/2.

See the “Shared options” section at the module documentation.

Examples

1 = MyCache.set(:a, 1)

1 = MyCache.pop(:a)
nil = MyCache.pop(:b)

%Nebulex.Object{key: :a, value: 1} =
  :a |> MyCache.set(1, return: :key) |> MyCache.pop(return: :object)

Callback implementation for Nebulex.Cache.pop/2.

Returns a list of hook functions that will be executed after invoke the cache action.

Examples

defmodule MyCache do
  use Nebulex.Cache, adapter: Nebulex.Adapters.Local

  def post_hooks do
    [&post_hook/2]
  end

  def post_hook(result, {_, :set, _} = call),
    do: send(:hooked_cache, call)
  def post_hook(_, _),
    do: :noop
end

Callback implementation for Nebulex.Cache.Hook.post_hooks/0.

Returns a list of hook functions that will be executed before invoke the cache action.

Examples

defmodule MyCache do
  use Nebulex.Cache, adapter: Nebulex.Adapters.Local

  def pre_hooks do
    pre_hook =
      fn
        (result, {_, :get, _} = call) ->
          # do your stuff ...
        (result, _) ->
          result
      end
    [pre_hook]
  end
end

Callback implementation for Nebulex.Cache.Hook.pre_hooks/0.

Link to this function reduce(acc, fun, opts \\ []) View Source

Invokes reducer for each entry in the cache, passing the key, the return and the accumulator acc as arguments. reducer’s return value is stored in acc.

Returns the accumulator.

Options

See the “Shared options” section at the module documentation.

Examples

for x <- 1..5, do: MyCache.set(x, x)

15 = MyCache.reduce(0, fn({_key, value}, acc) ->
  acc + value
end)

MyCache.reduce({%{}, 0}, fn({key, value}, {acc1, acc2}) ->
  if Map.has_key?(acc1, key),
    do: {acc1, acc2},
    else: {Map.put(acc1, key, value), value + acc2}
end)

WARNING: This is an expensive operation, beware of using it in prod.

Callback implementation for Nebulex.Cache.reduce/3.

Link to this function set(key, value, opts \\ []) View Source

Sets the given value under key in the Cache.

It returns value or Nebulex.Object.t (depends on :return option) if the value has been successfully inserted.

Options

  • :on_conflict - It may be one of :raise (the default), :nothing, :replace. See the “OnConflict” section for more information.

See the “Shared options” section at the module documentation.

Example

%Nebulex.Object{key: :a} =
  MyCache.set :a, "anything", ttl: 100000, return: :object

OnConflict

The :on_conflict option supports the following values:

  • :raise - raises if there is a conflicting key
  • :nothing - ignores the error in case of conflicts
  • :replace - replace the value on the existing key with the given value

Examples

# Set twice
%Nebulex.Object{key: :a, version: v1} = MyCache.set :a, 1, return: :object
%Nebulex.Object{key: :a, version: v2}
  MyCache.set :a, 2, return: :object, version: v1

# Set with the same key and wrong version but do nothing on conflicts.
# Keep in mind that, although this returns successfully, the returned
# struct does not reflect the data in the Cache. For instance, in case
# of "on_conflict: :nothing", the returned object isn't set.
%Nebulex.Object{key: :a, value: 2} =
  MyCache.set :a, 3, return: :object, version: v1, on_conflict: :nothing
2 = MyCache.get :a

# Set with the same key and wrong version but replace the current
# value on conflicts.
3 = MyCache.set :a, 3, version: v1, on_conflict: :replace
3 = MyCache.get :a

Callback implementation for Nebulex.Cache.set/3.

Returns the cache size (total number of cached entries).

Examples

for x <- 1..10, do: MyCache.set(x, x)

10 = MyCache.size

for x <- 1..5, do: MyCache.delete(x)

5 = MyCache.size

Callback implementation for Nebulex.Cache.size/0.

Starts a supervision and return {:ok, pid} or just :ok if nothing needs to be done.

Returns {:error, {:already_started, pid}} if the cache is already started or {:error, term} in case anything else goes wrong.

Options

See the configuration in the moduledoc for options shared between adapters, for adapter-specific configuration see the adapter’s documentation.

Callback implementation for Nebulex.Cache.start_link/1.

Link to this function stop(pid, timeout \\ 5000) View Source

Shuts down the cache represented by the given pid.

Callback implementation for Nebulex.Cache.stop/2.

Returns a map with all cache entries (key/value). If you want the map values be the cache object, pass the option :return set to :object.

Options

See the “Shared options” section at the module documentation.

Examples

for x <- 1..3, do: MyCache.set(x, x*2)

%{1 => 2, 2 => 4, 3 => 6} = MyCache.to_map

%Nebulex.Object{key: 1} = Map.get(MyCache.to_map(return: :object), 1)

WARNING: This is an expensive operation, beware of using it in prod.

Callback implementation for Nebulex.Cache.to_map/1.

Link to this function transaction(fun, opts \\ []) View Source

Runs the given function inside a transaction.

A successful transaction returns the value returned by the function.

Options

See the “Shared options” section at the module documentation.

Examples

MyCache.transaction fn ->
  1 = MyCache.set(:a, 1)
  true = MyCache.has_key?(:a)
  MyCache.get(:a)
end

Callback implementation for Nebulex.Cache.transaction/2.

Link to this function update(key, initial, fun, opts \\ []) View Source

Updates the cached key with the given function.

If key is present in Cache with value value, fun is invoked with argument value and its result is used as the new value of key.

If key is not present in Cache, initial is inserted as the value of key.

Options

  • :on_conflict - same as callback get/2.

Note that for this function :return option hasn’t any effect, since it always returns the object value.

See the “Shared options” section at the module documentation.

Examples

1 = MyCache.update(:a, 1, &(&1 * 2))

2 = MyCache.update(:a, 1, &(&1 * 2))

Callback implementation for Nebulex.Cache.update/4.

Link to this function update_counter(key, incr \\ 1, opts \\ []) View Source

Updates (increment or decrement) the counter mapped to the given key.

If incr >= 0 then the current value is incremented by that amount, otherwise the current value is decremented.

If incr is not a valid integer, then an ArgumentError exception is raised.

Options

See the “Shared options” section at the module documentation.

Examples

1 = MyCache.update_counter(:a)

3 = MyCache.update_counter(:a, 2)

2 = MyCache.update_counter(:a, -1)

%Nebulex.Object{key: :a, value: 2} = MyCache.update_counter(:a, 0, return: :object)

Callback implementation for Nebulex.Cache.update_counter/3.