Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yanick/dancer-plugin-cache-chi
Dancer plugin to cache response content (and anything else)
https://github.com/yanick/dancer-plugin-cache-chi
cache dancer perl
Last synced: 4 months ago
JSON representation
Dancer plugin to cache response content (and anything else)
- Host: GitHub
- URL: https://github.com/yanick/dancer-plugin-cache-chi
- Owner: yanick
- License: other
- Created: 2011-03-17T00:39:57.000Z (almost 14 years ago)
- Default Branch: releases
- Last Pushed: 2018-03-04T17:24:55.000Z (almost 7 years ago)
- Last Synced: 2024-09-30T15:13:09.524Z (4 months ago)
- Topics: cache, dancer, perl
- Language: Perl
- Homepage:
- Size: 92.8 KB
- Stars: 4
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.mkdn
- Changelog: Changes
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/yanick/Dancer-Plugin-Cache-CHI.svg?branch=master)](https://travis-ci.org/yanick/Dancer-Plugin-Cache-CHI)
[![AppVeyor Status](https://ci.appveyor.com/api/projects/status/github/yanick/Dancer-Plugin-Cache-CHI?branch=master&svg=true)](https://ci.appveyor.com/project/yanick/Dancer-Plugin-Cache-CHI)# NAME
Dancer::Plugin::Cache::CHI - Dancer plugin to cache response content (and anything else)
# VERSION
version 1.5.0
# SYNOPSIS
In your configuration:
```
plugins:
'Cache::CHI':
driver: Memory
global: 1
```In your application:
```perl
use Dancer ':syntax';
use Dancer::Plugin::Cache::CHI;# caching pages' response
check_page_cache;
get '/cache_me' => sub {
cache_page template 'foo';
};# using the helper functions
get '/clear' => sub {
cache_clear;
};put '/stash' => sub {
cache_set secret_stash => request->body;
};get '/stash' => sub {
return cache_get 'secret_stash';
};del '/stash' => {
return cache_remove 'secret_stash';
};# using the cache directly
get '/something' => sub {
my $thingy = cache->compute( 'thingy', sub { compute_thingy() } );return template 'foo' => { thingy => $thingy };
};
```# DESCRIPTION
This plugin provides Dancer with an interface to a [CHI](https://metacpan.org/pod/CHI) cache. Also, it
includes a mechanism to easily cache the response of routes.# CONFIGURATION
Unrecognized configuration elements are passed directly to the [CHI](https://metacpan.org/pod/CHI) object's
constructor. For example, the configuration given in the ["SYNOPSIS"](#synopsis)
will create a cache object equivalent to```perl
$cache = CHI->new( driver => 'Memory', global => 1, );
```## honor\_no\_cache
If the parameter '`honor_no_cache`' is set to true, a request with the http
header '`Cache-Control`' or '`Pragma`' set to '_no-cache_' will ignore any
content cached via '`cache_page`' and will have the page regenerated anew.# KEYWORDS
## cache
Returns the [CHI](https://metacpan.org/pod/CHI) cache object.
## cache $namespace, \\%args
[CHI](https://metacpan.org/pod/CHI) only allows one namespace per object. But you can create more caches by
using _cache $namespace, \\%args_. The new cache uses the arguments as defined in
the configuration, which values can be overriden by the optional arguments
(which are only used on the first invocation of the namespace).```perl
get '/memory' => sub {
cache('elephant')->get( 'stuff' );
};get '/goldfish' => sub {
cache( 'goldfish' => { expires_in => 300 } )->get( 'stuff' );
};
```Note that all the other keywords (`cache_page`, `cache_set`, etc) will still
use the main cache object.## check\_page\_cache
If invoked, returns the cached response of a route, if available.
The `path_info` attribute of the request is used as the key for the route,
so the same route requested with different parameters will yield the same
cached content. Caveat emptor.## cache\_page($content, $expiration)
Caches the _$content_ to be served to subsequent requests.
The headers and http status of the response are also cached.The _$expiration_ parameter is optional.
## cache\_page\_key
Returns the cache key used by '`cache_page`'. Defaults to
to the request's _path\_info_, but can be modified via
_cache\_page\_key\_generator_.## cache\_page\_key\_generator( \\&sub )
Sets the function that generates the cache key for _cache\_page_.
For example, to have the key contains both information about the request's
hostname and path\_info (useful to deal with multi-machine applications):```perl
cache_page_key_generator sub {
return join ':', request()->host, request()->path_info;
};
```## cache\_set, cache\_get, cache\_remove, cache\_clear, cache\_compute
Shortcut to the cache's object methods.
```perl
get '/cache/:attr/:value' => sub {
# equivalent to cache->set( ... );
cache_set $params->{attr} => $params->{value};
};
```See the [CHI](https://metacpan.org/pod/CHI) documentation for further info on these methods.
# HOOKS
## before\_create\_cache
Called before the creation of the cache, which is lazily done upon
its first use.Useful, for example, to change the cache's configuration at run time:
```perl
use Sys::Hostname;# set the namespace to the current hostname
hook before_create_cache => sub {
config->{plugins}{'Cache::CHI'}{namespace} = hostname;
};
```# SEE ALSO
Dancer Web Framework - [Dancer](https://metacpan.org/pod/Dancer)
[CHI](https://metacpan.org/pod/CHI)
[Dancer::Plugin::Memcached](https://metacpan.org/pod/Dancer::Plugin::Memcached) - plugin that heavily inspired this one.
[Dancer2::Plugin::Cache::CHI](https://metacpan.org/pod/Dancer2::Plugin::Cache::CHI) - Dancer2 incarnation of this plugin.
# AUTHOR
Yanick Champoux [![endorse](http://api.coderwall.com/yanick/endorsecount.png)](http://coderwall.com/yanick)
# COPYRIGHT AND LICENSE
This software is copyright (c) 2018, 2013, 2012, 2011 by Yanick Champoux.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.