-if ($usecache) {
- require Koha::Cache;
- Koha::Cache->import();
- $cache = Koha::Cache->new(
- {
- 'cache_type' => 'memcached',
- 'cache_servers' => $ENV{'MEMCACHED_SERVERS'}
+my $cache_active = Koha::Cache->is_cache_active;
+my ($cache_key, $cache, $json_text);
+if ($cache_active) {
+ $cache_key = "opac:report:".($report_name ? "name:$report_name" : "id:$report_id");
+ $cache = Koha::Cache->new();
+ $json_text = $cache->get_from_cache($cache_key);
+}
+
+unless ($json_text) {
+ my $offset = 0;
+ my $limit = C4::Context->preference("SvcMaxReportRows") || 10;
+ my ( $sth, $errors ) = execute_query( $report_rec->{savedsql}, $offset, $limit );
+ if ($sth) {
+ my $lines = $sth->fetchall_arrayref;
+ $json_text = to_json($lines);
+
+ if ($cache_active) {
+ $cache->set_in_cache( $cache_key, $json_text, $report_rec->{cache_expiry} );