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 10 10 100.0
pod 2 2 100.0
total 45 46 97.8


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