NAME

Mojo::Cache::Role::Strict - Limit get to keys that exist and prevent calls to set

STATUS

SYNOPSIS

  my $strict_cache = Mojo::Cache->new
                                ->set(key_that_exists => 'I am here!')
                                ->with_roles('+Strict')
                                ;

  # prints "I am here!"
  say $strict_cache->get('key_that_exists');

  # get key that doesn't exist dies
  say $strict_cache->get('nonexistent_key');

  # setting new key dies
  $strict_cache->set(new_key => 'I die!');

  # updating existing key dies
  $strict_cache->set(key_that_exists => 'I die!');

  # allow nonexistent keys to be passed to get
  my $value = $strict_cache->strict_get(0)->get('nonexistent_key');

  # allow keys to be set
  $strict_cache->strict_set(0)->set(new_key => 'I live!');

DESCRIPTION

Mojo::Cache::Role::Strict is a role that makes your Mojo::Cache instance strict by dying when calling "get" in Mojo::Cache with keys that do not exist in the cache (have not been set with "set" in Mojo::Cache) and by dying when you call "set" in Mojo::Cache. You can optionally allow "get" in Mojo::Cache and "set" in Mojo::Cache with "strict_get" and "strict_set".

METHODS

exists

  if ($strict_cache->exists('key')) {
    my $value = $strict_cache->get('key');
    ...
  }

Returns true if a cached value exists for the provided key, false otherwise.

"exists" is composed from Mojo::Cache::Role::Exists. See that module for more information.

strict_get

  my $strict_cache = Mojo::Cache->new->with_roles('+Strict')->strict_get(0);

  # lives even though key does not exist
  my $value = $strict_cache->get('nonexistent_key');

"strict_get" specifies whether keys must exist when calling "get" in Mojo::Cache. If true, keys that do not exist will throw. If false, undef will be returned.

The default is true.

This method returns the Mojo::Cache object.

strict_set

  my $strict_cache = Mojo::Cache->new
                                ->set(key_that_exists => 'I am here!')
                                ->with_roles('+Strict')
                                ->strict_set(0)
                                ;

  # setting new key lives
  $strict_cache->set(new_key => 'I live!');

  # updating existing key lives
  $strict_cache->set(key_that_exists => 'new value');

"strict_set" specifies whether "set" in Mojo::Cache may be called. If true, calling "set" in Mojo::Cache will throw. If false, calls to "set" in Mojo::Cache are allowed.

The default is true.

This method returns the Mojo::Cache object.

AUTHOR

Adam Hopkins <srchulo@cpan.org>

COPYRIGHT

Copyright 2019- Adam Hopkins

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO