Lines 18-24
package C4::Context;
Link Here
|
18 |
|
18 |
|
19 |
use strict; |
19 |
use strict; |
20 |
use warnings; |
20 |
use warnings; |
21 |
use vars qw($VERSION $AUTOLOAD $context @context_stack $servers $memcached $ismemcached); |
21 |
use vars qw($VERSION $AUTOLOAD $context @context_stack $memcached_servers); |
22 |
BEGIN { |
22 |
BEGIN { |
23 |
if ($ENV{'HTTP_USER_AGENT'}) { |
23 |
if ($ENV{'HTTP_USER_AGENT'}) { |
24 |
require CGI::Carp; |
24 |
require CGI::Carp; |
Lines 79-98
BEGIN {
Link Here
|
79 |
} # else there is no browser to send fatals to! |
79 |
} # else there is no browser to send fatals to! |
80 |
|
80 |
|
81 |
# Check if there are memcached servers set |
81 |
# Check if there are memcached servers set |
82 |
$servers = $ENV{'MEMCACHED_SERVERS'}; |
82 |
$memcached_servers = $ENV{'MEMCACHED_SERVERS'}; |
83 |
if ($servers) { |
83 |
# Load required libraries and create the memcached object |
84 |
# Load required libraries and create the memcached object |
84 |
require Cache::Memcached if $memcached_servers; |
85 |
require Cache::Memcached; |
|
|
86 |
$memcached = Cache::Memcached->new({ |
87 |
servers => [ $servers ], |
88 |
debug => 0, |
89 |
compress_threshold => 10_000, |
90 |
expire_time => 600, |
91 |
namespace => $ENV{'MEMCACHED_NAMESPACE'} || 'koha' |
92 |
}); |
93 |
# Verify memcached available (set a variable and test the output) |
94 |
$ismemcached = $memcached->set('ismemcached','1'); |
95 |
} |
96 |
|
85 |
|
97 |
$VERSION = '3.07.00.049'; |
86 |
$VERSION = '3.07.00.049'; |
98 |
} |
87 |
} |
Lines 109-114
use Carp;
Link Here
|
109 |
use C4::Boolean; |
98 |
use C4::Boolean; |
110 |
use C4::Debug; |
99 |
use C4::Debug; |
111 |
use Koha; |
100 |
use Koha; |
|
|
101 |
use Koha::Cache; |
112 |
use Koha::Config::SysPrefs; |
102 |
use Koha::Config::SysPrefs; |
113 |
|
103 |
|
114 |
=head1 NAME |
104 |
=head1 NAME |
Lines 209-237
sub current {
Link Here
|
209 |
return $context; |
199 |
return $context; |
210 |
} |
200 |
} |
211 |
|
201 |
|
212 |
=head2 ismemcached |
202 |
sub _new_memcached { |
|
|
203 |
my $namespace = shift or die "No memcached namespace"; |
204 |
|
205 |
return unless $memcached_servers; |
206 |
return Cache::Memcached->new({ |
207 |
servers => [ $memcached_servers ], |
208 |
debug => 0, |
209 |
compress_threshold => 10_000, |
210 |
expire_time => 600, |
211 |
namespace => $namespace |
212 |
}); |
213 |
} |
214 |
# Verify memcached available (test the output) |
215 |
sub _ping_memcached { |
216 |
my $memcached = shift or croak "No memcached"; |
217 |
|
218 |
return $memcached->set('ismemcached','1'); |
219 |
} |
220 |
|
221 |
=head2 cache |
213 |
|
222 |
|
214 |
Returns the value of the $ismemcached variable (0/1) |
223 |
Returns the cache object or undef |
215 |
|
224 |
|
216 |
=cut |
225 |
=cut |
217 |
|
226 |
|
218 |
sub ismemcached { |
227 |
sub cache { |
219 |
return $ismemcached; |
228 |
my $self = shift; |
|
|
229 |
|
230 |
$self = $context unless ref ($self); |
231 |
return unless $self; |
232 |
|
233 |
return $self->{cache} ||= Koha::Cache->new(); |
220 |
} |
234 |
} |
221 |
|
235 |
|
222 |
=head2 memcached |
236 |
=head2 memcached |
223 |
|
237 |
|
224 |
If $ismemcached is true, returns the $memcache variable. |
238 |
Returns the memcached object or undef |
225 |
Returns undef otherwise |
239 |
|
|
|
240 |
=head2 ismemcached |
226 |
|
241 |
|
227 |
=cut |
242 |
=cut |
228 |
|
243 |
|
229 |
sub memcached { |
244 |
sub memcached { |
230 |
if ($ismemcached) { |
245 |
my $self = shift; |
231 |
return $memcached; |
246 |
|
232 |
} else { |
247 |
$self = $context unless ref ($self); |
233 |
return; |
248 |
return unless $self; |
234 |
} |
249 |
|
|
|
250 |
my $memcached = $self->{memcached} or return; |
251 |
return _ping_memcached($memcached) ? $memcached : undef; |
252 |
} |
253 |
|
254 |
sub ismemcached { |
255 |
my $self = shift; |
256 |
return $self->memcached; |
235 |
} |
257 |
} |
236 |
|
258 |
|
237 |
sub db_driver { |
259 |
sub db_driver { |
Lines 273-282
sub import {
Link Here
|
273 |
# default context already exists? |
295 |
# default context already exists? |
274 |
return if $context; |
296 |
return if $context; |
275 |
|
297 |
|
276 |
if ($ismemcached) { |
298 |
return if $config_file && $config_file eq ":no_config"; |
|
|
299 |
|
300 |
my $memcached = _new_memcached($ENV{'MEMCACHED_NAMESPACE'} || 'koha'); |
301 |
if ($memcached) { |
277 |
# retrieve from memcached |
302 |
# retrieve from memcached |
278 |
if (my $self = $memcached->get('kohaconf')) { |
303 |
if ($context = $memcached->get('kohaconf')) { |
279 |
$context = $self; |
304 |
$context->{memcached} = $memcached; |
280 |
return; |
305 |
return; |
281 |
} |
306 |
} |
282 |
} |
307 |
} |
Lines 303-317
sub import {
Link Here
|
303 |
} |
328 |
} |
304 |
|
329 |
|
305 |
# no ? so load it! |
330 |
# no ? so load it! |
306 |
return if $config_file && $config_file eq ":no_config"; |
331 |
$context = $pkg->new($config_file) or return; |
307 |
my $new_ctx = __PACKAGE__->new($config_file); |
332 |
if ( $memcached && _ping_memcached($memcached) ) { |
308 |
return unless $new_ctx; |
333 |
$context->{memcached} = $memcached; |
309 |
|
334 |
$memcached->set('kohaconf',$context); |
310 |
# if successfully loaded, use it by default |
|
|
311 |
$context = $new_ctx; |
312 |
|
313 |
if ($ismemcached) { |
314 |
$memcached->set('kohaconf',$new_ctx); |
315 |
} |
335 |
} |
316 |
} |
336 |
} |
317 |
|
337 |
|
Lines 355-360
sub new {
Link Here
|
355 |
my $class = shift; |
375 |
my $class = shift; |
356 |
my $conf_fname = shift or croak "No conf"; |
376 |
my $conf_fname = shift or croak "No conf"; |
357 |
my $namespace = shift; |
377 |
my $namespace = shift; |
|
|
378 |
my $cache = shift; |
358 |
|
379 |
|
359 |
my $self = XMLin( |
380 |
my $self = XMLin( |
360 |
$conf_fname, |
381 |
$conf_fname, |
Lines 366-372
sub new {
Link Here
|
366 |
unless ref($self) && $self->{"config"}; |
387 |
unless ref($self) && $self->{"config"}; |
367 |
|
388 |
|
368 |
$self->{"config_file"} = $conf_fname; |
389 |
$self->{"config_file"} = $conf_fname; |
369 |
$self->{"namespace"} = $namespace; |
390 |
if ($namespace) { |
|
|
391 |
$self->{namespace} = $namespace; |
392 |
my $memcached = $context->{memcached}; |
393 |
$self->{memcached} = $memcached && $memcached->{namespace} eq $namespace |
394 |
? $memcached |
395 |
: _new_memcached($namespace); |
396 |
# Koha::Cache is far too complex to try to make any savings |
397 |
$cache ||= Koha::Cache->new( {namespace => $namespace} ); |
398 |
} |
399 |
$self->{cache} = $cache; |
370 |
|
400 |
|
371 |
$self->{"Zconn"} = undef; # Zebra Connections |
401 |
$self->{"Zconn"} = undef; # Zebra Connections |
372 |
$self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield |
402 |
$self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield |
Lines 427-434
sub set_context
Link Here
|
427 |
$schema = $self->{schema} ||= Koha::Database->new_schema($self); |
457 |
$schema = $self->{schema} ||= Koha::Database->new_schema($self); |
428 |
} |
458 |
} |
429 |
|
459 |
|
430 |
# Save the old context, if any, on the stack |
460 |
# Save the old context on the stack |
431 |
push @context_stack, $context if defined($context); |
461 |
push @context_stack, $context; |
432 |
|
462 |
|
433 |
# Set the new context |
463 |
# Set the new context |
434 |
$context = $new_context; |
464 |
$context = $new_context; |