File Coverage

blib/lib/Starch/Store/CHI.pm
Criterion Covered Total %
statement 32 32 100.0
branch 1 2 50.0
condition n/a
subroutine 11 11 100.0
pod 2 2 100.0
total 46 47 97.8


line stmt bran cond sub pod time code
1             package Starch::Store::CHI;
2 1     1   13909 use 5.008001;
  1         5  
3 1     1   7 use strictures 2;
  1         11  
  1         44  
4             our $VERSION = '0.05';
5              
6             =head1 NAME
7              
8             Starch::Store::CHI - Starch storage backend using CHI.
9              
10             =head1 SYNOPSIS
11              
12             my $starch = Starch->new(
13             store => {
14             class => '::CHI',
15             chi => {
16             driver => 'File',
17             root_dir => '/path/to/root',
18             },
19             },
20             ...,
21             );
22              
23             =head1 DESCRIPTION
24              
25             This L store uses L to set and get state data.
26              
27             =head1 EXCEPTIONS
28              
29             By default L will catch errors and log them using L
30             and keep on going as if nothing went wrong. In Starch, stores are
31             expected to loudly throw exceptions, so it is suggested that you
32             specify these arguments to your CHI driver:
33              
34             on_get_error => 'die',
35             on_set_error => 'die',
36              
37             And then, if you still want the errors logged, you can use
38             L. This is especially
39             important if you are using the L
40             plugin which will throw an exception when the timeout is exceeded
41             which then CHI will catch and log by default, which is not what
42             you want.
43              
44             =head1 PERFORMANCE
45              
46             When using CHI there are various choices you need to make:
47              
48             =over
49              
50             =item *
51              
52             Which backend to use? If data persistence is not an issue, or
53             you're using CHI as your outer store in L
54             then Memcached or Redis are common solutions which have high
55             performance.
56              
57             =item *
58              
59             Which serializer to use? Nowadays L is the serialization
60             performance heavyweight, with L coming up a close second.
61              
62             =item *
63              
64             Which driver to use? Some backends have more than one driver, and
65             some drivers perform better than others. The most common example of
66             this is Memcached which has three drivers which can be used with
67             CHI.
68              
69             =back
70              
71             Make sure you ask these questions when you implement CHI for
72             Starch, and take the time to answer them well. It can make a big
73             difference.
74              
75             =cut
76              
77 1     1   1021 use CHI;
  1         35755  
  1         45  
78 1     1   11 use Types::Standard -types;
  1         2  
  1         24  
79 1     1   4826 use Types::Common::String -types;
  1         2  
  1         10  
80 1     1   1491 use Scalar::Util qw( blessed );
  1         2  
  1         52  
81              
82 1     1   7 use Moo;
  1         2  
  1         8  
83 1     1   454 use namespace::clean;
  1         2  
  1         9  
84              
85             with qw(
86             Starch::Store
87             );
88              
89             after BUILD => sub{
90             my ($self) = @_;
91              
92             # Get this loaded as early as possible.
93             $self->chi();
94              
95             return;
96             };
97              
98             =head1 REQUIRED ARGUMENTS
99              
100             =head2 chi
101              
102             This must be set to either hash ref arguments for L or a
103             pre-built CHI object (often retrieved using a method proxy).
104              
105             When configuring Starch from static configuration files using a
106             L
107             is a good way to link your existing L object constructor
108             in with Starch so that starch doesn't build its own.
109              
110             =cut
111              
112             has _chi_arg => (
113             is => 'ro',
114             isa => (InstanceOf[ 'CHI::Driver' ]) | HashRef,
115             init_arg => 'chi',
116             required => 1,
117             );
118              
119             has chi => (
120             is => 'lazy',
121             isa => InstanceOf[ 'CHI::Driver' ],
122             init_arg => undef,
123             );
124             sub _build_chi {
125 5     5   62 my ($self) = @_;
126              
127 5         22 my $chi = $self->_chi_arg();
128 5 50       27 return $chi if blessed $chi;
129              
130 5         42 return CHI->new( %$chi );
131             }
132              
133             =head1 METHODS
134              
135             =head2 set
136              
137             Set L.
138              
139             =head2 get
140              
141             Set L.
142              
143             =head2 remove
144              
145             Set L.
146              
147             =cut
148              
149             sub set {
150             my ($self, $id, $namespace, $data, $expires) = @_;
151              
152             $self->chi->set(
153             $self->stringify_key( $id, $namespace ),
154             $data,
155             $expires ? ($expires) : (),
156             );
157              
158             return;
159             }
160              
161             sub get {
162 19     19 1 14279 my ($self, $id, $namespace) = @_;
163              
164 19         366 return $self->chi->get(
165             $self->stringify_key( $id, $namespace ),
166             );
167             }
168              
169             sub remove {
170 6     6 1 4385 my ($self, $id, $namespace) = @_;
171              
172 6         113 $self->chi->remove(
173             $self->stringify_key( $id, $namespace ),
174             );
175              
176 6         774 return;
177             }
178              
179             1;
180             __END__