| 1 | # Copyright Infomation | ||
| 2 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 3 | # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D. | ||
| 4 | # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com | ||
| 5 | # Email : mewsoft@cpan.org, support@mewsoft.com | ||
| 6 | # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved. | ||
| 7 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 8 | package Nile::Plugin::Cache; | ||
| 9 | |||
| 10 | our $VERSION = '0.55'; | ||
| 11 | our $AUTHORITY = 'cpan:MEWSOFT'; | ||
| 12 | |||
| 13 | =pod | ||
| 14 | |||
| 15 | =encoding utf8 | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Nile::Plugin::Cache - Cache plugin for the Nile framework. | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | my $cash = $app->plugin->cache; | ||
| 24 | |||
| 25 | # save visitors count to the cache | ||
| 26 | $cash->set("visitor_count", $cash->get("visitor_count") + 1, "1 year"); | ||
| 27 | |||
| 28 | # retrieve visitors count from the cache | ||
| 29 | $view->set("visitor_count", $cash->get("visitor_count")); | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | Nile::Plugin::Cache - Cache plugin for the Nile framework. | ||
| 34 | |||
| 35 | Returns the L<CHI> object. All L<CHI> methods are supported. | ||
| 36 | |||
| 37 | Plugin settings in th config file under C<plugin> section. The C<autoload> variable is must be set to true value for the plugin to be loaded | ||
| 38 | on application startup to setup hooks to work before actions dispatch: | ||
| 39 | |||
| 40 | <plugin> | ||
| 41 | |||
| 42 | <cache> | ||
| 43 | <autoload>0</autoload> | ||
| 44 | <driver>File</driver> | ||
| 45 | <root_dir></root_dir> | ||
| 46 | <namespace>cache</namespace> | ||
| 47 | </cache> | ||
| 48 | |||
| 49 | </plugin> | ||
| 50 | |||
| 51 | For DBI driver configuration example: | ||
| 52 | |||
| 53 | <driver>DBI</driver> | ||
| 54 | <namespace>cache</namespace> | ||
| 55 | <table_prefix>cache_</table_prefix> | ||
| 56 | <create_table>1</create_table> | ||
| 57 | |||
| 58 | The DBI create table example: | ||
| 59 | |||
| 60 | CREATE TABLE <table_prefix><namespace> ( | ||
| 61 | `key` VARCHAR(...), | ||
| 62 | `value` TEXT, | ||
| 63 | PRIMARY KEY (`key`) | ||
| 64 | ) | ||
| 65 | |||
| 66 | The driver will try to create the table if you set C<create_table> in the config and table does not exist. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | use Nile::Plugin; # also extends Nile::Plugin | ||
| 71 | |||
| 72 | use CHI; | ||
| 73 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 74 | sub main { | ||
| 75 | |||
| 76 | my ($self, $arg) = @_; | ||
| 77 | |||
| 78 | my $app = $self->app; | ||
| 79 | my $setting = $self->setting(); | ||
| 80 | |||
| 81 | my $driver = $setting->{cache} || +{}; | ||
| 82 | |||
| 83 | $driver->{driver} ||= "File"; | ||
| 84 | $driver->{namespace} ||= "cache"; # default namespace is Default | ||
| 85 | |||
| 86 | if (!$driver->{root_dir}) { | ||
| 87 | $driver->{root_dir} = $app->file->catdir($app->var->get("cache_dir"), $driver->{namespace}); | ||
| 88 | } | ||
| 89 | |||
| 90 | if ($driver->{driver} eq "DBI") { | ||
| 91 | $driver->{dbh} = $app->dbh(); | ||
| 92 | } | ||
| 93 | |||
| 94 | rebless => CHI->new(%{$driver}); | ||
| 95 | } | ||
| 96 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 97 | |||
| 98 | =pod | ||
| 99 | |||
| 100 | =head1 Bugs | ||
| 101 | |||
| 102 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||
| 103 | |||
| 104 | =head1 HOMEPAGE | ||
| 105 | |||
| 106 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||
| 107 | |||
| 108 | =head1 SOURCE | ||
| 109 | |||
| 110 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||
| 111 | |||
| 112 | =head1 SEE ALSO | ||
| 113 | |||
| 114 | See L<Nile> for details about the complete framework. | ||
| 115 | |||
| 116 | =head1 AUTHOR | ||
| 117 | |||
| 118 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||
| 119 | Website: http://www.mewsoft.com | ||
| 120 | |||
| 121 | =head1 COPYRIGHT AND LICENSE | ||
| 122 | |||
| 123 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||
| 124 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||
| 125 | |||
| 126 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | 1; |