Cache.Sandbox (elixir_cache v0.3.11)

View Source

Sandbox adapter for isolated testing of applications using ElixirCache.

This module provides a mock implementation of all cache adapters, allowing tests to run in isolation without interfering with each other's data. The sandbox uses a basic Agent to store data in memory and implements the full range of caching operations supported by all adapters.

Features

  • Isolated cache namespaces for concurrent testing
  • Support for all standard cache operations
  • Implementation of Redis-specific features like hash and JSON operations
  • Implementation of ETS-specific operations for complete testing compatibility
  • Lightweight in-memory storage for fast test execution

Usage

The Sandbox adapter is typically enabled through the sandbox? option when defining a cache module:

defmodule MyApp.TestCache do
  use Cache,
    adapter: Cache.Redis,  # Original adapter doesn't matter when sandbox is enabled
    name: :test_cache,
    opts: [],
    sandbox?: Mix.env() == :test
end

In your tests, use Cache.SandboxRegistry.start(MyApp.TestCache) in the setup block to ensure proper isolation between test cases.

Note: This adapter should not be used in production environments.

Summary

Functions

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

command(cache_name, command, opts)

command!(cache_name, command, opts)

from_dets(cache_name, dets_table)

from_ets(cache_name, ets_table)

hash_delete(cache_name, key, hash_key, opts)

hash_get(cache_name, key, hash_key, opts)

hash_get_all(cache_name, key, opts)

hash_get_many(cache_name, keys_fields, opts)

hash_scan(cache_name, key, scan_opts, opts)

hash_set(cache_name, key, field, value, ttl, opts)

hash_set_many(cache_name, keys_fields_values, ttl, opts)

hash_values(cache_name, key, opts)

info(cache_name)

info(cache_name, item)

insert_raw(cache_name, data)

json_array_append(cache_name, key, path, value, opts)

json_clear(cache_name, key, path, opts)

json_delete(cache_name, key, path, opts)

json_get(cache_name, key, path, opts)

json_incr(cache_name, key, path, incr \\ 1, opts)

json_set(cache_name, key, path, value, opts)

match_delete(cache_name, pattern)

match_object(cache_name, pattern)

match_object(cache_name, pattern, limit)

member(cache_name, key)

pipeline(cache_name, commands, opts)

pipeline!(cache_name, commands, opts)

sadd(cache_name, key, value, opts)

scan(cache_name, scan_opts, opts)

select(cache_name, match_spec)

select(cache_name, match_spec, limit)

select_delete(cache_name, match_spec)

smembers(cache_name, key, opts)

to_dets(cache_name, dets_table)

to_ets(cache_name)

update_counter(cache_name, key, arg)