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($AUTOLOAD $context @context_stack $servers $memcached $ismemcached); |
21 |
use vars qw($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 88-107
BEGIN {
Link Here
|
88 |
} # else there is no browser to send fatals to! |
88 |
} # else there is no browser to send fatals to! |
89 |
|
89 |
|
90 |
# Check if there are memcached servers set |
90 |
# Check if there are memcached servers set |
91 |
$servers = $ENV{'MEMCACHED_SERVERS'}; |
91 |
$memcached_servers = $ENV{'MEMCACHED_SERVERS'}; |
92 |
if ($servers) { |
92 |
# Load required libraries and create the memcached object |
93 |
# Load required libraries and create the memcached object |
93 |
require Cache::Memcached if $memcached_servers; |
94 |
require Cache::Memcached; |
|
|
95 |
$memcached = Cache::Memcached->new({ |
96 |
servers => [ $servers ], |
97 |
debug => 0, |
98 |
compress_threshold => 10_000, |
99 |
expire_time => 600, |
100 |
namespace => $ENV{'MEMCACHED_NAMESPACE'} || 'koha' |
101 |
}); |
102 |
# Verify memcached available (set a variable and test the output) |
103 |
$ismemcached = $memcached->set('ismemcached','1'); |
104 |
} |
105 |
|
94 |
|
106 |
} |
95 |
} |
107 |
|
96 |
|
Lines 219-247
sub current {
Link Here
|
219 |
return $context; |
208 |
return $context; |
220 |
} |
209 |
} |
221 |
|
210 |
|
222 |
=head2 ismemcached |
211 |
sub _new_memcached { |
|
|
212 |
my $namespace = shift or die "No memcached namespace"; |
213 |
|
214 |
return unless $memcached_servers; |
215 |
return Cache::Memcached->new({ |
216 |
servers => [ $memcached_servers ], |
217 |
debug => 0, |
218 |
compress_threshold => 10_000, |
219 |
expire_time => 600, |
220 |
namespace => $namespace || $ENV{'MEMCACHED_NAMESPACE'} || 'koha' |
221 |
}); |
222 |
} |
223 |
# Verify memcached available (test the output) |
224 |
sub _ping_memcached { |
225 |
my $memcached = shift or croak "No memcached"; |
223 |
|
226 |
|
224 |
Returns the value of the $ismemcached variable (0/1) |
227 |
return $memcached->set('ismemcached','1'); |
|
|
228 |
} |
229 |
|
230 |
=head2 cache |
231 |
|
232 |
Returns the cache object or undef |
225 |
|
233 |
|
226 |
=cut |
234 |
=cut |
227 |
|
235 |
|
228 |
sub ismemcached { |
236 |
sub cache { |
229 |
return $ismemcached; |
237 |
my $self = shift; |
|
|
238 |
$self = $context unless ref ($self); |
239 |
|
240 |
return $self->{cache}; |
230 |
} |
241 |
} |
231 |
|
242 |
|
232 |
=head2 memcached |
243 |
=head2 memcached |
233 |
|
244 |
|
234 |
If $ismemcached is true, returns the $memcache variable. |
245 |
Returns the memcached object or undef |
235 |
Returns undef otherwise |
246 |
|
|
|
247 |
=head2 ismemcached |
236 |
|
248 |
|
237 |
=cut |
249 |
=cut |
238 |
|
250 |
|
239 |
sub memcached { |
251 |
sub memcached { |
240 |
if ($ismemcached) { |
252 |
my $self = shift; |
241 |
return $memcached; |
253 |
$self = $context unless ref ($self); |
242 |
} else { |
254 |
|
243 |
return; |
255 |
my $memcached = $self->{memcached} or return; |
244 |
} |
256 |
return _ping_memcached($memcached) ? $memcached : undef; |
|
|
257 |
} |
258 |
|
259 |
sub ismemcached { |
260 |
my $self = shift; |
261 |
return $self->memcached; |
245 |
} |
262 |
} |
246 |
|
263 |
|
247 |
sub db_driver { |
264 |
sub db_driver { |
Lines 285-294
sub import {
Link Here
|
285 |
# default context already exists? |
302 |
# default context already exists? |
286 |
return if $context; |
303 |
return if $context; |
287 |
|
304 |
|
288 |
if ($ismemcached) { |
305 |
return if $config_file && $config_file eq ":no_config"; |
|
|
306 |
|
307 |
my $memcached = _new_memcached($ENV{'MEMCACHED_NAMESPACE'} || 'koha'); |
308 |
if ($memcached) { |
289 |
# retrieve from memcached |
309 |
# retrieve from memcached |
290 |
if (my $self = $memcached->get('kohaconf')) { |
310 |
if ($context = $memcached->get('kohaconf')) { |
291 |
$context = $self; |
311 |
$context->{memcached} = $memcached; |
|
|
312 |
$context->{cache} = Koha::Cache->new({namespace => $context->{namespace}}); |
292 |
return; |
313 |
return; |
293 |
} |
314 |
} |
294 |
} |
315 |
} |
Lines 315-330
sub import {
Link Here
|
315 |
} |
336 |
} |
316 |
|
337 |
|
317 |
# no ? so load it! |
338 |
# no ? so load it! |
318 |
return if $config_file && $config_file eq ":no_config"; |
339 |
$context = $pkg->_new($config_file) or return; |
319 |
my $new_ctx = __PACKAGE__->new($config_file); |
340 |
if ( $memcached && _ping_memcached($memcached) ) { |
320 |
return unless $new_ctx; |
341 |
$memcached->set('kohaconf',$context); |
321 |
|
342 |
# Canot serialize cache objects |
322 |
# if successfully loaded, use it by default |
343 |
$context->{memcached} = $memcached; |
323 |
$context = $new_ctx; |
|
|
324 |
|
325 |
if ($ismemcached) { |
326 |
$memcached->set('kohaconf',$new_ctx); |
327 |
} |
344 |
} |
|
|
345 |
$context->{cache} = Koha::Cache->new({namespace => $context->{namespace}}); |
328 |
} |
346 |
} |
329 |
|
347 |
|
330 |
use Scalar::Util qw(openhandle); |
348 |
use Scalar::Util qw(openhandle); |
Lines 366-371
sub new {
Link Here
|
366 |
my $conf_fname = shift or croak "No conf"; |
384 |
my $conf_fname = shift or croak "No conf"; |
367 |
my $namespace = shift; |
385 |
my $namespace = shift; |
368 |
|
386 |
|
|
|
387 |
my $self = $class->_new($conf_fname, $namespace); |
388 |
|
389 |
if ($memcached_servers) { |
390 |
$self->{memcached} = _new_memcached($namespace); |
391 |
} |
392 |
$self->{cache} = Koha::Cache->new({namespace => $namespace}); |
393 |
|
394 |
return $self; |
395 |
} |
396 |
|
397 |
sub _new { |
398 |
my $class = shift; |
399 |
my $conf_fname = shift or croak "No conf"; |
400 |
my $namespace = shift; |
401 |
|
369 |
my $self = XMLin( |
402 |
my $self = XMLin( |
370 |
$conf_fname, |
403 |
$conf_fname, |
371 |
keyattr => ['id'], |
404 |
keyattr => ['id'], |
Lines 378-384
sub new {
Link Here
|
378 |
$self->{config_file} = $conf_fname; |
411 |
$self->{config_file} = $conf_fname; |
379 |
$self->{namespace} = $namespace; |
412 |
$self->{namespace} = $namespace; |
380 |
$self->{use_syspref_cache} = 1; |
413 |
$self->{use_syspref_cache} = 1; |
381 |
$self->{syspref_cache} = Koha::Cache->new({namespace => $namespace}); |
|
|
382 |
|
414 |
|
383 |
$self->{"Zconn"} = undef; # Zebra Connections |
415 |
$self->{"Zconn"} = undef; # Zebra Connections |
384 |
$self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield |
416 |
$self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield |
Lines 559-565
sub preference {
Link Here
|
559 |
if defined $ENV{"OVERRIDE_SYSPREF_$var"}; |
591 |
if defined $ENV{"OVERRIDE_SYSPREF_$var"}; |
560 |
|
592 |
|
561 |
my $cached_var = $self->{use_syspref_cache} |
593 |
my $cached_var = $self->{use_syspref_cache} |
562 |
? $self->{syspref_cache}->get_from_cache("syspref_$var") |
594 |
? $self->cache->get_from_cache("syspref_$var") |
563 |
: undef; |
595 |
: undef; |
564 |
return $cached_var if defined $cached_var; |
596 |
return $cached_var if defined $cached_var; |
565 |
|
597 |
|
Lines 568-574
sub preference {
Link Here
|
568 |
my $value = $syspref ? $syspref->value() : undef; |
600 |
my $value = $syspref ? $syspref->value() : undef; |
569 |
|
601 |
|
570 |
if ( $self->{use_syspref_cache} ) { |
602 |
if ( $self->{use_syspref_cache} ) { |
571 |
$self->{syspref_cache}->set_in_cache("syspref_$var", $value); |
603 |
$self->cache->set_in_cache("syspref_$var", $value); |
|
|
604 |
$self->{sysprefs}{$var} = $value if $self; |
572 |
} |
605 |
} |
573 |
return $value; |
606 |
return $value; |
574 |
} |
607 |
} |
Lines 609-616
used with Plack and other persistent environments.
Link Here
|
609 |
sub disable_syspref_cache { |
642 |
sub disable_syspref_cache { |
610 |
my ($self) = @_; |
643 |
my ($self) = @_; |
611 |
$self = $context unless ref $self; |
644 |
$self = $context unless ref $self; |
612 |
$self->{use_syspref_cache} = 0; |
|
|
613 |
$self->clear_syspref_cache(); |
645 |
$self->clear_syspref_cache(); |
|
|
646 |
$self->{use_syspref_cache} = 0; |
614 |
} |
647 |
} |
615 |
|
648 |
|
616 |
=head2 clear_syspref_cache |
649 |
=head2 clear_syspref_cache |
Lines 627-633
sub clear_syspref_cache {
Link Here
|
627 |
my ($self) = @_; |
660 |
my ($self) = @_; |
628 |
$self = $context unless ref $self; |
661 |
$self = $context unless ref $self; |
629 |
return unless $self->{use_syspref_cache}; |
662 |
return unless $self->{use_syspref_cache}; |
630 |
$self->{syspref_cache}->flush_all; |
663 |
$self->cache->flush_all; |
631 |
} |
664 |
} |
632 |
|
665 |
|
633 |
=head2 set_preference |
666 |
=head2 set_preference |
Lines 680-686
sub set_preference {
Link Here
|
680 |
} |
713 |
} |
681 |
|
714 |
|
682 |
if ( $self->{use_syspref_cache} ) { |
715 |
if ( $self->{use_syspref_cache} ) { |
683 |
$self->{syspref_cache}->set_in_cache( "syspref_$variable", $value ); |
716 |
$self->cache->set_in_cache( "syspref_$variable", $value ); |
684 |
} |
717 |
} |
685 |
|
718 |
|
686 |
return $syspref; |
719 |
return $syspref; |
Lines 702-708
sub delete_preference {
Link Here
|
702 |
|
735 |
|
703 |
if ( Koha::Config::SysPrefs->find( $var )->delete ) { |
736 |
if ( Koha::Config::SysPrefs->find( $var )->delete ) { |
704 |
if ( $self->{use_syspref_cache} ) { |
737 |
if ( $self->{use_syspref_cache} ) { |
705 |
$self->{syspref_cache}->clear_from_cache("syspref_$var"); |
738 |
$self->cache->clear_from_cache("syspref_$var"); |
706 |
} |
739 |
} |
707 |
|
740 |
|
708 |
return 1; |
741 |
return 1; |