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); |
|
|
22 |
|
23 |
use Koha::Cache; |
22 |
|
24 |
|
23 |
BEGIN { |
25 |
BEGIN { |
24 |
if ($ENV{'HTTP_USER_AGENT'}) { |
26 |
if ($ENV{'HTTP_USER_AGENT'}) { |
Lines 79-100
BEGIN {
Link Here
|
79 |
} |
81 |
} |
80 |
} # else there is no browser to send fatals to! |
82 |
} # else there is no browser to send fatals to! |
81 |
|
83 |
|
82 |
# Check if there are memcached servers set |
|
|
83 |
$servers = $ENV{'MEMCACHED_SERVERS'}; |
84 |
if ($servers) { |
85 |
# Load required libraries and create the memcached object |
86 |
require Cache::Memcached; |
87 |
$memcached = Cache::Memcached->new({ |
88 |
servers => [ $servers ], |
89 |
debug => 0, |
90 |
compress_threshold => 10_000, |
91 |
expire_time => 600, |
92 |
namespace => $ENV{'MEMCACHED_NAMESPACE'} || 'koha' |
93 |
}); |
94 |
# Verify memcached available (set a variable and test the output) |
95 |
$ismemcached = $memcached->set('ismemcached','1'); |
96 |
} |
97 |
|
98 |
$VERSION = '3.00.00.036'; |
84 |
$VERSION = '3.00.00.036'; |
99 |
} |
85 |
} |
100 |
|
86 |
|
Lines 248-285
Returns undef in case of error.
Link Here
|
248 |
sub read_config_file { # Pass argument naming config file to read |
234 |
sub read_config_file { # Pass argument naming config file to read |
249 |
my $koha = XMLin(shift, keyattr => ['id'], forcearray => ['listen', 'server', 'serverinfo'], suppressempty => ''); |
235 |
my $koha = XMLin(shift, keyattr => ['id'], forcearray => ['listen', 'server', 'serverinfo'], suppressempty => ''); |
250 |
|
236 |
|
251 |
if ($ismemcached) { |
237 |
if (Koha::Cache->is_cache_active()) { |
252 |
$memcached->set('kohaconf',$koha); |
238 |
my $cache = Koha::Cache->new(); |
|
|
239 |
$cache->set_in_cache('kohaconf', $koha) if defined $cache; |
253 |
} |
240 |
} |
254 |
|
241 |
|
255 |
return $koha; # Return value: ref-to-hash holding the configuration |
242 |
return $koha; # Return value: ref-to-hash holding the configuration |
256 |
} |
243 |
} |
257 |
|
244 |
|
258 |
=head2 ismemcached |
|
|
259 |
|
260 |
Returns the value of the $ismemcached variable (0/1) |
261 |
|
262 |
=cut |
263 |
|
264 |
sub ismemcached { |
265 |
return $ismemcached; |
266 |
} |
267 |
|
268 |
=head2 memcached |
269 |
|
270 |
If $ismemcached is true, returns the $memcache variable. |
271 |
Returns undef otherwise |
272 |
|
273 |
=cut |
274 |
|
275 |
sub memcached { |
276 |
if ($ismemcached) { |
277 |
return $memcached; |
278 |
} else { |
279 |
return undef; |
280 |
} |
281 |
} |
282 |
|
283 |
# db_scheme2dbi |
245 |
# db_scheme2dbi |
284 |
# Translates the full text name of a database into de appropiate dbi name |
246 |
# Translates the full text name of a database into de appropiate dbi name |
285 |
# |
247 |
# |
Lines 323-331
Allocates a new context. Initializes the context from the specified
Link Here
|
323 |
file, which defaults to either the file given by the C<$KOHA_CONF> |
285 |
file, which defaults to either the file given by the C<$KOHA_CONF> |
324 |
environment variable, or F</etc/koha/koha-conf.xml>. |
286 |
environment variable, or F</etc/koha/koha-conf.xml>. |
325 |
|
287 |
|
326 |
It saves the koha-conf.xml values in the declared memcached server(s) |
288 |
It saves the koha-conf.xml values in the cache (if configured) and uses |
327 |
if currently available and uses those values until them expire and |
289 |
those values until them expire and re-reads them. |
328 |
re-reads them. |
|
|
329 |
|
290 |
|
330 |
C<&new> does not set this context as the new default context; for |
291 |
C<&new> does not set this context as the new default context; for |
331 |
that, use C<&set_context>. |
292 |
that, use C<&set_context>. |
Lines 362-376
sub new {
Link Here
|
362 |
} |
323 |
} |
363 |
} |
324 |
} |
364 |
|
325 |
|
365 |
if ($ismemcached) { |
326 |
if (Koha::Cache->is_cache_active()) { |
366 |
# retreive from memcached |
327 |
# retrieve from cache |
367 |
$self = $memcached->get('kohaconf'); |
328 |
my $cache = Koha::Cache->new(); |
368 |
if (not defined $self) { |
329 |
$self = $cache->get_from_cache('kohaconf') if defined $cache; |
369 |
# not in memcached yet |
330 |
$self = { }; |
370 |
$self = read_config_file($conf_fname); |
331 |
} |
371 |
} |
332 |
if (!keys %$self) { |
372 |
} else { |
333 |
# not cached yet |
373 |
# non-memcached env, read from file |
|
|
374 |
$self = read_config_file($conf_fname); |
334 |
$self = read_config_file($conf_fname); |
375 |
} |
335 |
} |
376 |
|
336 |
|
Lines 527-537
my %sysprefs;
Link Here
|
527 |
sub preference { |
487 |
sub preference { |
528 |
my $self = shift; |
488 |
my $self = shift; |
529 |
my $var = lc(shift); # The system preference to return |
489 |
my $var = lc(shift); # The system preference to return |
|
|
490 |
my $cache; |
530 |
|
491 |
|
531 |
if (exists $sysprefs{$var}) { |
492 |
if (exists $sysprefs{$var}) { |
532 |
return $sysprefs{$var}; |
493 |
return $sysprefs{$var}; |
533 |
} |
494 |
} |
534 |
|
495 |
|
|
|
496 |
if (Koha::Cache->is_cache_active()) { |
497 |
$cache = Koha::Cache->new(); |
498 |
if (defined $cache) { |
499 |
$sysprefs{$var} = $cache->get_from_cache("syspref:$var"); |
500 |
return $sysprefs{$var} if (defined $sysprefs{$var}); |
501 |
} |
502 |
} |
503 |
|
535 |
my $dbh = C4::Context->dbh or return 0; |
504 |
my $dbh = C4::Context->dbh or return 0; |
536 |
|
505 |
|
537 |
# Look up systempreferences.variable==$var |
506 |
# Look up systempreferences.variable==$var |
Lines 542-547
sub preference {
Link Here
|
542 |
LIMIT 1 |
511 |
LIMIT 1 |
543 |
END_SQL |
512 |
END_SQL |
544 |
$sysprefs{$var} = $dbh->selectrow_array( $sql, {}, $var ); |
513 |
$sysprefs{$var} = $dbh->selectrow_array( $sql, {}, $var ); |
|
|
514 |
if (Koha::Cache->is_cache_active() && defined $cache) { |
515 |
$cache->set_in_cache("syspref:$var"); |
516 |
} |
545 |
return $sysprefs{$var}; |
517 |
return $sysprefs{$var}; |
546 |
} |
518 |
} |
547 |
|
519 |
|
Lines 564-569
will not be seen by this process.
Link Here
|
564 |
|
536 |
|
565 |
sub clear_syspref_cache { |
537 |
sub clear_syspref_cache { |
566 |
%sysprefs = (); |
538 |
%sysprefs = (); |
|
|
539 |
if (Koha::Cache->is_cache_active()) { |
540 |
my $cache = Koha::Cache->new(); |
541 |
$cache->flush_all() if defined $cache; # Sorry, this is unpleasant |
542 |
} |
567 |
} |
543 |
} |
568 |
|
544 |
|
569 |
=head2 set_preference |
545 |
=head2 set_preference |
Lines 594-599
sub set_preference {
Link Here
|
594 |
" ); |
570 |
" ); |
595 |
|
571 |
|
596 |
if($sth->execute( $var, $value )) { |
572 |
if($sth->execute( $var, $value )) { |
|
|
573 |
if (Koha::Cache->is_cache_active()) { |
574 |
my $cache = Koha::Cache->new(); |
575 |
$cache->set_in_cache("syspref:$var", $value) if defined $cache; |
576 |
} |
597 |
$sysprefs{$var} = $value; |
577 |
$sysprefs{$var} = $value; |
598 |
} |
578 |
} |
599 |
$sth->finish; |
579 |
$sth->finish; |