Main Koha release repository https://koha-community.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

119 lines
3.0 KiB

  1. package Koha::Template::Plugin::Cache;
  2. # Copyright Catalyst IT 2011
  3. # This file is part of Koha.
  4. #
  5. # Koha is free software; you can redistribute it and/or modify it
  6. # under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # Koha is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Koha; if not, see <http://www.gnu.org/licenses>.
  17. use strict;
  18. use warnings;
  19. use base qw( Template::Plugin );
  20. use Template::Plugin;
  21. use C4::Context;
  22. #------------------------------------------------------------------------
  23. # new(\%options)
  24. #------------------------------------------------------------------------
  25. sub new {
  26. my ( $class, $context, $params ) = @_;
  27. my $cache;
  28. if ( $params->{cache} ) {
  29. $cache = delete $params->{cache};
  30. }
  31. else {
  32. require Koha::Cache;
  33. $cache = Koha::Caches->get_instance();
  34. }
  35. my $self = bless {
  36. CACHE => $cache,
  37. CONFIG => $params,
  38. CONTEXT => $context,
  39. }, $class;
  40. return $self;
  41. }
  42. #------------------------------------------------------------------------
  43. # $cache->include({
  44. # template => 'foo.html',
  45. # keys => {'user.name', user.name},
  46. # ttl => 60, #seconds
  47. # });
  48. #------------------------------------------------------------------------
  49. sub inc {
  50. my ( $self, $params ) = @_;
  51. $self->_cached_action( 'include', $params );
  52. }
  53. sub proc {
  54. my ( $self, $params ) = @_;
  55. $self->_cached_action( 'process', $params );
  56. }
  57. sub _cached_action {
  58. my ( $self, $action, $params ) = @_;
  59. my $key;
  60. if ( $params->{key} ) {
  61. $key = delete $params->{key};
  62. }
  63. else {
  64. my $cache_keys = $params->{keys};
  65. $key = join(
  66. ':',
  67. (
  68. $params->{template},
  69. map { "$_=$cache_keys->{$_}" } keys %{$cache_keys}
  70. )
  71. );
  72. }
  73. my $result = $self->{CACHE}->get_from_cache($key);
  74. if ( !$result ) {
  75. $result = $self->{CONTEXT}->$action( $params->{template} );
  76. $self->{CACHE}
  77. ->set_in_cache( $key, $result, { expiry => $params->{ttl} } );
  78. }
  79. return $result;
  80. }
  81. 1;
  82. =head1 NAME
  83. Koha::Template::Plugin::Cache - cache output of templates
  84. =head1 SYNOPSIS
  85. [% USE cache = Cache %]
  86. [% cache.inc(
  87. 'template' => 'slow.html',
  88. 'keys' => {'user.name' => user.name},
  89. 'ttl' => 360
  90. ) %]
  91. # or with a pre-defined Cache::* object and key
  92. [% USE cache = Cache( cache => mycache ) %]
  93. [% cache.inc(
  94. 'template' => 'slow.html',
  95. 'key' => mykey,
  96. 'ttl' => 360
  97. ) %]
  98. __END__