use strict;
use warnings;
-use vars qw($VERSION $AUTOLOAD $context @context_stack);
+use vars qw($VERSION $AUTOLOAD $context @context_stack $servers $memcached $ismemcached);
BEGIN {
if ($ENV{'HTTP_USER_AGENT'}) {
$main::SIG{__DIE__} = \&CGI::Carp::confess;
}
} # else there is no browser to send fatals to!
- $VERSION = '3.00.00.036';
+
+ # Check if there are memcached servers set
+ $servers = $ENV{'MEMCACHED_SERVERS'};
+ if ($servers) {
+ # Load required libraries and create the memcached object
+ require Cache::Memcached;
+ $memcached = Cache::Memcached->new({
+ servers => [ $servers ],
+ debug => 0,
+ compress_threshold => 10_000,
+ expire_time => 600,
+ namespace => $ENV{'MEMCACHED_NAMESPACE'} || 'koha'
+ });
+ # Verify memcached available (set a variable and test the output)
+ $ismemcached = $memcached->set('ismemcached','1');
+ }
+
+ $VERSION = '3.00.00.036';
}
use DBI;
use C4::Boolean;
use C4::Debug;
use POSIX ();
+use DateTime::TimeZone;
=head1 NAME
sub read_config_file { # Pass argument naming config file to read
my $koha = XMLin(shift, keyattr => ['id'], forcearray => ['listen', 'server', 'serverinfo'], suppressempty => '');
+
+ if ($ismemcached) {
+ $memcached->set('kohaconf',$koha);
+ }
+
return $koha; # Return value: ref-to-hash holding the configuration
}
+=head2 ismemcached
+
+Returns the value of the $ismemcached variable (0/1)
+
+=cut
+
+sub ismemcached {
+ return $ismemcached;
+}
+
+=head2 memcached
+
+If $ismemcached is true, returns the $memcache variable.
+Returns undef otherwise
+
+=cut
+
+sub memcached {
+ if ($ismemcached) {
+ return $memcached;
+ } else {
+ return undef;
+ }
+}
+
# db_scheme2dbi
# Translates the full text name of a database into de appropiate dbi name
#
sub db_scheme2dbi {
my $name = shift;
-
+ # for instance, we support only mysql, so don't care checking
+ return "mysql";
for ($name) {
# FIXME - Should have other databases.
- if (/mysql/i) { return("mysql"); }
+ if (/mysql/) { return("mysql"); }
if (/Postgres|Pg|PostgresSQL/) { return("Pg"); }
- if (/oracle/i) { return("Oracle"); }
+ if (/oracle/) { return("Oracle"); }
}
return undef; # Just in case
}
file, which defaults to either the file given by the C<$KOHA_CONF>
environment variable, or F</etc/koha/koha-conf.xml>.
+It saves the koha-conf.xml values in the declared memcached server(s)
+if currently available and uses those values until them expire and
+re-reads them.
+
C<&new> does not set this context as the new default context; for
that, use C<&set_context>.
return undef;
}
}
- # Load the desired config file.
- $self = read_config_file($conf_fname);
- $self->{"config_file"} = $conf_fname;
+ if ($ismemcached) {
+ # retreive from memcached
+ $self = $memcached->get('kohaconf');
+ if (not defined $self) {
+ # not in memcached yet
+ $self = read_config_file($conf_fname);
+ }
+ } else {
+ # non-memcached env, read from file
+ $self = read_config_file($conf_fname);
+ }
+
+ $self->{"config_file"} = $conf_fname;
warn "read_config_file($conf_fname) returned undef" if !defined($self->{"config"});
return undef if !defined($self->{"config"});
$self->{"userenv"} = undef; # User env
$self->{"activeuser"} = undef; # current active user
$self->{"shelves"} = undef;
+ $self->{tz} = undef; # local timezone object
bless $self, $class;
return $self;
sub preference {
my $self = shift;
- my $var = shift; # The system preference to return
+ my $var = lc(shift); # The system preference to return
if (exists $sysprefs{$var}) {
return $sysprefs{$var};
sub set_preference {
my $self = shift;
- my $var = shift;
+ my $var = lc(shift);
my $value = shift;
my $dbh = C4::Context->dbh or return 0;
ON DUPLICATE KEY UPDATE value = VALUES(value)
" );
- $sth->execute( $var, $value );
+ if($sth->execute( $var, $value )) {
+ $sysprefs{$var} = $value;
+ }
$sth->finish;
}
}
+=head2 tz
+
+ C4::Context->tz
+
+ Returns a DateTime::TimeZone object for the system timezone
+
+=cut
+
+sub tz {
+ my $self = shift;
+ if (!defined $context->{tz}) {
+ $context->{tz} = DateTime::TimeZone->new(name => 'local');
+ }
+ return $context->{tz};
+}
+
+
+
1;
__END__