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 211-239
sub current {
Link Here
|
211 |
return $context; |
200 |
return $context; |
212 |
} |
201 |
} |
213 |
|
202 |
|
214 |
=head2 ismemcached |
203 |
sub _new_memcached { |
|
|
204 |
my $namespace = shift or die "No memcached namespace"; |
215 |
|
205 |
|
216 |
Returns the value of the $ismemcached variable (0/1) |
206 |
return unless $memcached_servers; |
|
|
207 |
return Cache::Memcached->new({ |
208 |
servers => [ $memcached_servers ], |
209 |
debug => 0, |
210 |
compress_threshold => 10_000, |
211 |
expire_time => 600, |
212 |
namespace => $namespace |
213 |
}); |
214 |
} |
215 |
# Verify memcached available (test the output) |
216 |
sub _ping_memcached { |
217 |
my $memcached = shift or croak "No memcached"; |
218 |
|
219 |
return $memcached->set('ismemcached','1'); |
220 |
} |
221 |
|
222 |
=head2 cache |
223 |
|
224 |
Returns the cache object or undef |
217 |
|
225 |
|
218 |
=cut |
226 |
=cut |
219 |
|
227 |
|
220 |
sub ismemcached { |
228 |
sub cache { |
221 |
return $ismemcached; |
229 |
my $self = shift; |
|
|
230 |
|
231 |
$self = $context unless ref ($self); |
232 |
return unless $self; |
233 |
|
234 |
return $self->{cache} ||= Koha::Cache->new(); |
222 |
} |
235 |
} |
223 |
|
236 |
|
224 |
=head2 memcached |
237 |
=head2 memcached |
225 |
|
238 |
|
226 |
If $ismemcached is true, returns the $memcache variable. |
239 |
Returns the memcached object or undef |
227 |
Returns undef otherwise |
240 |
|
|
|
241 |
=head2 ismemcached |
228 |
|
242 |
|
229 |
=cut |
243 |
=cut |
230 |
|
244 |
|
231 |
sub memcached { |
245 |
sub memcached { |
232 |
if ($ismemcached) { |
246 |
my $self = shift; |
233 |
return $memcached; |
247 |
|
234 |
} else { |
248 |
$self = $context unless ref ($self); |
235 |
return; |
249 |
return unless $self; |
236 |
} |
250 |
|
|
|
251 |
my $memcached = $self->{memcached} or return; |
252 |
return _ping_memcached($memcached) ? $memcached : undef; |
253 |
} |
254 |
|
255 |
sub ismemcached { |
256 |
my $self = shift; |
257 |
return $self->memcached; |
237 |
} |
258 |
} |
238 |
|
259 |
|
239 |
sub db_driver { |
260 |
sub db_driver { |
Lines 277-286
sub import {
Link Here
|
277 |
# default context already exists? |
298 |
# default context already exists? |
278 |
return if $context; |
299 |
return if $context; |
279 |
|
300 |
|
280 |
if ($ismemcached) { |
301 |
return if $config_file && $config_file eq ":no_config"; |
|
|
302 |
|
303 |
my $memcached = _new_memcached($ENV{'MEMCACHED_NAMESPACE'} || 'koha'); |
304 |
if ($memcached) { |
281 |
# retrieve from memcached |
305 |
# retrieve from memcached |
282 |
if (my $self = $memcached->get('kohaconf')) { |
306 |
if ($context = $memcached->get('kohaconf')) { |
283 |
$context = $self; |
307 |
$context->{memcached} = $memcached; |
284 |
return; |
308 |
return; |
285 |
} |
309 |
} |
286 |
} |
310 |
} |
Lines 307-321
sub import {
Link Here
|
307 |
} |
331 |
} |
308 |
|
332 |
|
309 |
# no ? so load it! |
333 |
# no ? so load it! |
310 |
return if $config_file && $config_file eq ":no_config"; |
334 |
$context = $pkg->new($config_file) or return; |
311 |
my $new_ctx = __PACKAGE__->new($config_file); |
335 |
if ( $memcached && _ping_memcached($memcached) ) { |
312 |
return unless $new_ctx; |
336 |
$context->{memcached} = $memcached; |
313 |
|
337 |
$memcached->set('kohaconf',$context); |
314 |
# if successfully loaded, use it by default |
|
|
315 |
$context = $new_ctx; |
316 |
|
317 |
if ($ismemcached) { |
318 |
$memcached->set('kohaconf',$new_ctx); |
319 |
} |
338 |
} |
320 |
} |
339 |
} |
321 |
|
340 |
|
Lines 357-362
sub new {
Link Here
|
357 |
my $class = shift; |
376 |
my $class = shift; |
358 |
my $conf_fname = shift or croak "No conf"; |
377 |
my $conf_fname = shift or croak "No conf"; |
359 |
my $namespace = shift; |
378 |
my $namespace = shift; |
|
|
379 |
my $cache = shift; |
360 |
|
380 |
|
361 |
my $self = XMLin( |
381 |
my $self = XMLin( |
362 |
$conf_fname, |
382 |
$conf_fname, |
Lines 368-374
sub new {
Link Here
|
368 |
unless ref($self) && $self->{"config"}; |
388 |
unless ref($self) && $self->{"config"}; |
369 |
|
389 |
|
370 |
$self->{"config_file"} = $conf_fname; |
390 |
$self->{"config_file"} = $conf_fname; |
371 |
$self->{"namespace"} = $namespace; |
391 |
if ($namespace) { |
|
|
392 |
$self->{namespace} = $namespace; |
393 |
my $memcached = $context->{memcached}; |
394 |
$self->{memcached} = $memcached && $memcached->{namespace} eq $namespace |
395 |
? $memcached |
396 |
: _new_memcached($namespace); |
397 |
# Koha::Cache is far too complex to try to make any savings |
398 |
$cache ||= Koha::Cache->new( {namespace => $namespace} ); |
399 |
} |
400 |
$self->{cache} = $cache; |
372 |
|
401 |
|
373 |
$self->{"Zconn"} = undef; # Zebra Connections |
402 |
$self->{"Zconn"} = undef; # Zebra Connections |
374 |
$self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield |
403 |
$self->{"marcfromkohafield"} = undef; # the hash with relations between koha table fields and MARC field/subfield |
Lines 428-435
sub set_context
Link Here
|
428 |
$schema = $self->{schema} ||= Koha::Database->new_schema($self); |
457 |
$schema = $self->{schema} ||= Koha::Database->new_schema($self); |
429 |
} |
458 |
} |
430 |
|
459 |
|
431 |
# Save the old context, if any, on the stack |
460 |
# Save the old context on the stack |
432 |
push @context_stack, $context if defined($context); |
461 |
push @context_stack, $context; |
433 |
|
462 |
|
434 |
# Set the new context |
463 |
# Set the new context |
435 |
$context = $new_context; |
464 |
$context = $new_context; |
Lines 541-547
with this method.
Link Here
|
541 |
|
570 |
|
542 |
=cut |
571 |
=cut |
543 |
|
572 |
|
544 |
my $syspref_cache = Koha::Cache->get_instance(); |
|
|
545 |
my $use_syspref_cache = 1; |
573 |
my $use_syspref_cache = 1; |
546 |
sub preference { |
574 |
sub preference { |
547 |
my $self = shift; |
575 |
my $self = shift; |
Lines 554-560
sub preference {
Link Here
|
554 |
return $self->{sysprefs}{$var} if $use_syspref_cache && $self && exists $self->{sysprefs}{$var}; |
582 |
return $self->{sysprefs}{$var} if $use_syspref_cache && $self && exists $self->{sysprefs}{$var}; |
555 |
|
583 |
|
556 |
my $cached_var = $use_syspref_cache |
584 |
my $cached_var = $use_syspref_cache |
557 |
? $syspref_cache->get_from_cache("syspref_$var") |
585 |
? $self->cache->get_from_cache("syspref_$var") |
558 |
: undef; |
586 |
: undef; |
559 |
return $cached_var if defined $cached_var; |
587 |
return $cached_var if defined $cached_var; |
560 |
|
588 |
|
Lines 566-572
sub preference {
Link Here
|
566 |
} |
594 |
} |
567 |
|
595 |
|
568 |
if ( $use_syspref_cache ) { |
596 |
if ( $use_syspref_cache ) { |
569 |
$syspref_cache->set_in_cache("syspref_$var", $value); |
597 |
$self->cache->set_in_cache("syspref_$var", $value); |
570 |
$self->{sysprefs}{$var} = $value if $self; |
598 |
$self->{sysprefs}{$var} = $value if $self; |
571 |
} |
599 |
} |
572 |
return $value; |
600 |
return $value; |
Lines 625-633
sub clear_syspref_cache {
Link Here
|
625 |
|
653 |
|
626 |
$self = $context unless ref $self; |
654 |
$self = $context unless ref $self; |
627 |
|
655 |
|
628 |
return unless $use_syspref_cache; |
656 |
$self->cache->flush_all; |
629 |
|
|
|
630 |
$syspref_cache->flush_all; |
631 |
$self->{sysprefs} = {} if $self; |
657 |
$self->{sysprefs} = {} if $self; |
632 |
} |
658 |
} |
633 |
|
659 |
|
Lines 682-688
sub set_preference {
Link Here
|
682 |
} |
708 |
} |
683 |
|
709 |
|
684 |
if ( $use_syspref_cache ) { |
710 |
if ( $use_syspref_cache ) { |
685 |
$syspref_cache->set_in_cache( "syspref_$variable", $value ); |
711 |
$self->cache->set_in_cache( "syspref_$variable", $value ); |
686 |
$self->{sysprefs}{$variable} = $value if $self; |
712 |
$self->{sysprefs}{$variable} = $value if $self; |
687 |
} |
713 |
} |
688 |
|
714 |
|
Lines 706-712
sub delete_preference {
Link Here
|
706 |
|
732 |
|
707 |
if ( Koha::Config::SysPrefs->find( $var )->delete ) { |
733 |
if ( Koha::Config::SysPrefs->find( $var )->delete ) { |
708 |
if ( $use_syspref_cache ) { |
734 |
if ( $use_syspref_cache ) { |
709 |
$syspref_cache->clear_from_cache("syspref_$var"); |
735 |
$self->cache->clear_from_cache("syspref_$var"); |
710 |
delete $self->{sysprefs}{$var} if $self; |
736 |
delete $self->{sysprefs}{$var} if $self; |
711 |
} |
737 |
} |
712 |
|
738 |
|