You are here

panels_simple_cache.module in Panels 6.2

Same filename and directory in other branches
  1. 5.2 panels_simple_cache/panels_simple_cache.module

panels_simple_cache.module

Provides a simple time-based caching option for panel panes.

File

panels_simple_cache/panels_simple_cache.module
View source
<?php

/**
 * @file panels_simple_cache.module
 *
 * Provides a simple time-based caching option for panel panes.
 */

/**
 * Implementation of hook_menu().
 */
function panels_simple_cache_menu() {
  $items['admin/panels/simple-cache'] = array(
    'title' => 'Simple cache',
    'access' => 'panels_simple_cache_access',
    'type' => MENU_NORMAL_ITEM,
    'callback' => 'panels_simple_cache_admin',
    'description' => 'Information about Panels simple cache.',
  );
  return $items;
}

/**
 * Access callback for the simple_cache administration menu.
 */
function panels_simple_cache_access() {
  return user_access('access administration pages') && user_access('use panels caching features');
}

/**
 * Page callback for the very short admin page.
 */
function panels_simple_cache_admin() {
  $output = '<p>';
  $output .= t('Panels simple cache does not have a normal administrative UI, such as panels pages or mini panels. With this module, you are given the option to add caching features to any panel display in panel pages, mini panels, panel nodes or any other displays provided by other modules or plugins. These options are available as an icon on each pane of the edit content pane.');
  $output .= '</p><p>';
  $output .= t('This module provides only very simple, time-based caching; it is not at all suitable if your content will change at all per user (and this can mean administrative additions that are just visible to you) as all users will see the same content; it is provided mostly as a reference implementation for other, smarter caching modules.');
  $output .= '</p>';
  return $output;
}

/**
 * Implementation of hook_panels_cache()
 */
function panels_simple_cache_panels_cache() {
  $cache['simple'] = array(
    'title' => t("Simple cache"),
    'description' => t('Simple caching is a time-based cache. This is a hard limit, and once cached it will remain that way until the time limit expires.'),
    'cache get' => 'panels_simple_cache_get_cache',
    'cache set' => 'panels_simple_cache_set_cache',
    'cache clear' => 'panels_simple_cache_clear_cache',
    'settings form' => 'panels_simple_cache_settings_form',
    'settings form submit' => 'panels_simple_cache_settings_form_submit',
  );
  return $cache;
}

/**
 * Get cached content.
 */
function panels_simple_cache_get_cache($conf, $display, $args, $contexts, $pane = NULL) {
  $cid = panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane);
  $cache = cache_get($cid, 'cache');
  if (!$cache) {
    return FALSE;
  }
  if (time() - $cache->created > $conf['lifetime']) {
    return FALSE;
  }
  return unserialize($cache->data);
}

/**
 * Set cached content.
 */
function panels_simple_cache_set_cache($conf, $content, $display, $args, $contexts, $pane = NULL) {
  $cid = panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane);
  cache_set($cid, $content);
}

/**
 * Clear cached content.
 *
 * Cache clears are always for an entire display, regardless of arguments.
 */
function panels_simple_cache_clear_cache($display) {
  $cid = 'panels_simple_cache';

  // This is used in case this is an in-code display, which means did will be something like 'new-1'.
  if (isset($display->owner) && isset($display->owner->id)) {
    $cid .= ':' . $display->owner->id;
  }
  $cid .= ':' . $display->did;
  cache_clear_all($cid, 'cache', TRUE);
}

/**
 * Figure out an id for our cache based upon input and settings.
 */
function panels_simple_cache_get_id($conf, $display, $args, $contexts, $pane) {
  $id = 'panels_simple_cache';

  // This is used in case this is an in-code display, which means did will be something like 'new-1'.
  if (isset($display->owner) && isset($display->owner->id)) {
    $id .= ':' . $display->owner->id;
  }
  $id .= ':' . $display->did;
  if ($pane) {
    $id .= ':' . $pane->pid;
  }
  if (user_access('view pane admin links')) {
    $id .= ':admin';
  }
  switch ($conf['granularity']) {
    case 'args':
      foreach ($args as $arg) {
        $id .= ':' . $arg;
      }
      break;
    case 'context':
      if (!is_array($contexts)) {
        $contexts = array(
          $contexts,
        );
      }
      foreach ($contexts as $context) {
        if (isset($context->argument)) {
          $id .= ':' . $context->argument;
        }
      }
  }
  return $id;
}
function panels_simple_cache_settings_form($conf, $display, $pid) {
  $options = drupal_map_assoc(array(
    15,
    30,
    60,
    120,
    180,
    240,
    300,
    600,
    900,
    1200,
    1800,
    3600,
    7200,
    14400,
    28800,
    43200,
    86400,
    172800,
    259200,
    345600,
    604800,
  ), 'format_interval');
  $form['lifetime'] = array(
    '#title' => t('Lifetime'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => $conf['lifetime'],
  );
  $form['granularity'] = array(
    '#title' => t('Granularity'),
    '#type' => t('select'),
    '#options' => array(
      'args' => t('Arguments'),
      'context' => t('Context'),
      'none' => t('None'),
    ),
    '#description' => t('If "arguments" are selected, this content will be cached per individual argument to the entire display; if "contexts" are selected, this content will be cached per unique context in the pane or display; if "neither" there will be only one cache for this pane.'),
    '#default_value' => $conf['granularity'],
  );
  return $form;
}

Functions

Namesort descending Description
panels_simple_cache_access Access callback for the simple_cache administration menu.
panels_simple_cache_admin Page callback for the very short admin page.
panels_simple_cache_clear_cache Clear cached content.
panels_simple_cache_get_cache Get cached content.
panels_simple_cache_get_id Figure out an id for our cache based upon input and settings.
panels_simple_cache_menu Implementation of hook_menu().
panels_simple_cache_panels_cache Implementation of hook_panels_cache()
panels_simple_cache_settings_form
panels_simple_cache_set_cache Set cached content.