Scroll to navigation

Mojolicious::Plugin::CHI(3) User Contributed Perl Documentation Mojolicious::Plugin::CHI(3)

NAME

Mojolicious::Plugin::CHI - Use CHI Caches in Mojolicious

SYNOPSIS

  # Mojolicious
  $app->plugin(CHI => {
    MyCache => {
      driver     => 'FastMmap',
      root_dir   => '/cache',
      cache_size => '20m'
    }
  });
  # Mojolicious::Lite
  plugin 'CHI' => {
    default => {
      driver => 'Memory',
      global => 1
    }
  };
  # In Controllers:
  $c->chi('MyCache')->set(my_key => 'This is my value');
  print $c->chi('MyCache')->get('my_key');
  # Using the default cache
  $c->chi->set(from_memory => 'With love!');
  print $c->chi->get('from_memory');

DESCRIPTION

Mojolicious::Plugin::CHI is a simple plugin to work with CHI caches within Mojolicious.

METHODS

register

  # Mojolicious
  $app->plugin(CHI => {
    MyCache => {
      driver     => 'FastMmap',
      root_dir   => '/cache',
      cache_size => '20m'
    },
    default => {
      driver => 'Memory',
      global => 1
    },
    namespaces => 1
  });
  # Mojolicious::Lite
  plugin 'CHI' => {
    default => { driver => 'Memory', global => 1 }
  };
  # Or in your config file
  {
    CHI => {
      default => {
        driver => 'Memory',
        global => 1
      }
    }
  }

Called when registering the plugin. On creation, the plugin accepts a hash of cache names associated with CHI objects. All cache handles are qualified CHI namespaces. You can omit this mapping by passing a "namespaces" parameter with a "false" value. The handles have to be unique, i.e. you can't have multiple different "default" caches in mounted applications using Mojolicious::Plugin::Mount. Logging defaults to the application log, but can be overridden using on_get_error and on_set_error.

All parameters can be set as part of the configuration file with the key "CHI" or on registration (that can be overwritten by configuration).

Use custom CHI subclasses by passing a "chi_class" parameter with the class name of a CHI subclass.

HELPERS

chi

  # In Controllers:
  $c->chi('MyCache')->set(my_key => 'This is my value', '10 min');
  print $c->chi('MyCache')->get('my_key');
  print $c->chi->get('from_default_cache');

Returns a CHI handle if registered. Accepts the name of the registered cache. If no cache handle name is given, a cache handle name "default" is assumed.

COMMANDS

The following commands are available when the plugin is registered.

chi list

  perl app.pl chi list

List all CHI caches associated with your application.

chi purge

  perl app.pl chi purge mycache

Remove all expired entries from the cache namespace.

chi clear

  perl app.pl chi clear mycache

Remove all entries from the cache namespace.

chi expire

  perl app.pl chi expire mykey
  perl app.pl chi expire mycache mykey

Set the expiration date of a key to the past. This does not necessarily delete the data, but makes it unavailable using "get".

chi remove

  perl app.pl chi remove mykey
  perl app.pl chi remove mycache mykey

Remove a key from the cache.

DEPENDENCIES

Mojolicious, CHI.

Note: Old versions of CHI had a lot of dependencies. It was thus not recommended to use this plugin in a CGI environment. Since new versions of CHI use Moo instead of Moose, more use cases may be possible.

CONTRIBUTORS

Boris Däppen <https://github.com/borisdaeppen>

Renée Bäcker <https://github.com/reneeb>

Rouzier <https://github.com/rouzier>

Mohammad S Anwar <https://github.com/manwar>

AVAILABILITY

  https://github.com/Akron/Mojolicious-Plugin-CHI

COPYRIGHT AND LICENSE

Copyright (C) 2012-2018, Nils Diewald <http://nils-diewald.de/>.

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

2018-11-28 perl v5.40.0