File Coverage

lib/Templer/Plugin/Redis.pm
Criterion Covered Total %
statement 24 29 82.7
branch 2 6 33.3
condition 1 6 16.6
subroutine 5 5 100.0
pod 1 2 50.0
total 33 48 68.7


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             Templer::Plugin::Redis - A plugin to retrieve values from Redis
5              
6             =cut
7              
8             =head1 SYNOPSIS
9              
10             The following is a good example use of this plugin
11              
12             title: About my site
13             count: redis_get( "total_count" )
14             ----
15            

There are ponies.

16              
17             =cut
18              
19             =head1 DESCRIPTION
20              
21             This plugin allows template variables to be values retrieved from
22             a redis store.
23              
24             It is assumed that redis will be running on the localhost, if it is
25             not you may set the environmental variable C to point
26             to your IP:port pair.
27              
28             =cut
29              
30             =head1 LICENSE
31              
32             This module is free software; you can redistribute it and/or modify it
33             under the terms of either:
34              
35             a) the GNU General Public License as published by the Free Software
36             Foundation; either version 2, or (at your option) any later version,
37             or
38              
39             b) the Perl "Artistic License".
40              
41             =cut
42              
43             =head1 AUTHOR
44              
45             Steve Kemp
46              
47             =cut
48              
49             =head1 COPYRIGHT AND LICENSE
50              
51             Copyright (C) 2015 Steve Kemp .
52              
53             This library is free software. You can modify and or distribute it under
54             the same terms as Perl itself.
55              
56             =cut
57              
58             =head1 METHODS
59              
60             =cut
61              
62              
63 11     11   5248 use strict;
  11         17  
  11         252  
64 11     11   44 use warnings;
  11         16  
  11         3267  
65              
66              
67             package Templer::Plugin::Redis;
68              
69              
70             =head2
71              
72             Constructor. No arguments are required/supported.
73              
74             =cut
75              
76             sub new
77             {
78 11     11 0 20 my ( $proto, %supplied ) = (@_);
79 11   33     55 my $class = ref($proto) || $proto;
80              
81 11         19 my $self = {};
82              
83              
84 11         21 bless( $self, $class );
85              
86 11         13 my $module = "use Redis;";
87              
88             ## no critic (Eval)
89 11     11   6396 eval($module);
  11         961748  
  11         242  
  11         623  
90             ## use critic
91              
92 11 50       60 if ( !$@ )
93             {
94              
95             #
96             # OK the module was loaded, but Redis might not be
97             # running locally, or accessible remotely so in those
98             # cases we'll be disabled.
99             #
100             # NOTE: Redis will use $ENV{'REDIS_SERVER'} if that is
101             # set, otherwise defaulting to 127.0.0.1:6379.
102             #
103 11         26 eval {$self->{ 'redis' } = new Redis()};
  11         52  
104             }
105              
106 11         10565 return $self;
107             }
108              
109              
110             =head2 expand_variables
111              
112             This is the method which is called by the L
113             to expand the variables contained in a L object.
114              
115             Variables are written in the file in the form "key: value", and are
116             internally stored within the Page object as a hash.
117              
118             This method iterates over each key & value and updates any that
119             seem to refer to redis-fetches.
120              
121             =cut
122              
123             sub expand_variables
124             {
125 9     9 1 33 my ( $self, $site, $page, $data ) = (@_);
126              
127             #
128             # Get the page-variables in the template.
129             #
130 9         93 my %hash = %$data;
131              
132             #
133             # Look for a value of "redis_get" in each key.
134             #
135 9         56 foreach my $key ( keys %hash )
136             {
137 66 50       148 if ( $hash{ $key } =~ /^redis_get\((.*)\)/ )
138             {
139              
140             #
141             # The lookup value.
142             #
143 0         0 my $rkey = $1;
144              
145             #
146             # Strip leading/trailing whitespace and quotes
147             #
148 0         0 $rkey =~ s/^\s+|\s+$//g;
149 0         0 $rkey =~ s/^["']|['"]$//g;
150              
151             #
152             # If we have redis, and it is alive/connected, then use it.
153             #
154 0 0 0     0 if ( $self->{ 'redis' } &&
155             $self->{ 'redis' }->ping() )
156             {
157 0         0 $hash{ $key } = $self->{ 'redis' }->get($rkey);
158             }
159             }
160              
161             }
162              
163             #
164             # Return.
165             #
166 9         25 return ( \%hash );
167             }
168              
169              
170             #
171             # Register the plugin.
172             #
173             Templer::Plugin::Factory->new()->register_plugin("Templer::Plugin::Redis");