3 namespace Drupal\views\Plugin\views\cache;
5 use Drupal\Core\Datetime\DateFormatterInterface;
6 use Drupal\Core\Cache\Cache;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Symfony\Component\HttpFoundation\Request;
12 * Simple caching of query results for Views displays.
14 * @ingroup views_cache_plugins
18 * title = @Translation("Time-based"),
19 * help = @Translation("Simple time-based caching of data.")
22 class Time extends CachePluginBase {
27 protected $usesOptions = TRUE;
30 * The date formatter service.
32 * @var \Drupal\Core\Datetime\DateFormatterInterface
34 protected $dateFormatter;
37 * The current request.
39 * @var \Symfony\Component\HttpFoundation\Request
44 * Constructs a Time cache plugin object.
46 * @param array $configuration
47 * A configuration array containing information about the plugin instance.
48 * @param string $plugin_id
49 * The plugin_id for the plugin instance.
50 * @param mixed $plugin_definition
51 * The plugin implementation definition.
52 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
53 * The date formatter service.
54 * @param \Symfony\Component\HttpFoundation\Request $request
55 * The current request.
57 public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter, Request $request) {
58 $this->dateFormatter = $date_formatter;
59 $this->request = $request;
61 parent::__construct($configuration, $plugin_id, $plugin_definition);
67 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
72 $container->get('date.formatter'),
73 $container->get('request_stack')->getCurrentRequest()
77 protected function defineOptions() {
78 $options = parent::defineOptions();
79 $options['results_lifespan'] = ['default' => 3600];
80 $options['results_lifespan_custom'] = ['default' => 0];
81 $options['output_lifespan'] = ['default' => 3600];
82 $options['output_lifespan_custom'] = ['default' => 0];
87 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
88 parent::buildOptionsForm($form, $form_state);
89 $options = [60, 300, 1800, 3600, 21600, 518400];
90 $options = array_map([$this->dateFormatter, 'formatInterval'], array_combine($options, $options));
91 $options = [0 => $this->t('Never cache')] + $options + ['custom' => $this->t('Custom')];
93 $form['results_lifespan'] = [
95 '#title' => $this->t('Query results'),
96 '#description' => $this->t('The length of time raw query results should be cached.'),
97 '#options' => $options,
98 '#default_value' => $this->options['results_lifespan'],
100 $form['results_lifespan_custom'] = [
101 '#type' => 'textfield',
102 '#title' => $this->t('Seconds'),
104 '#maxlength' => '30',
105 '#description' => $this->t('Length of time in seconds raw query results should be cached.'),
106 '#default_value' => $this->options['results_lifespan_custom'],
109 ':input[name="cache_options[results_lifespan]"]' => ['value' => 'custom'],
113 $form['output_lifespan'] = [
115 '#title' => $this->t('Rendered output'),
116 '#description' => $this->t('The length of time rendered HTML output should be cached.'),
117 '#options' => $options,
118 '#default_value' => $this->options['output_lifespan'],
120 $form['output_lifespan_custom'] = [
121 '#type' => 'textfield',
122 '#title' => $this->t('Seconds'),
124 '#maxlength' => '30',
125 '#description' => $this->t('Length of time in seconds rendered HTML output should be cached.'),
126 '#default_value' => $this->options['output_lifespan_custom'],
129 ':input[name="cache_options[output_lifespan]"]' => ['value' => 'custom'],
135 public function validateOptionsForm(&$form, FormStateInterface $form_state) {
136 $custom_fields = ['output_lifespan', 'results_lifespan'];
137 foreach ($custom_fields as $field) {
138 $cache_options = $form_state->getValue('cache_options');
139 if ($cache_options[$field] == 'custom' && !is_numeric($cache_options[$field . '_custom'])) {
140 $form_state->setError($form[$field . '_custom'], $this->t('Custom time values must be numeric.'));
145 public function summaryTitle() {
146 $results_lifespan = $this->getLifespan('results');
147 $output_lifespan = $this->getLifespan('output');
148 return $this->dateFormatter->formatInterval($results_lifespan, 1) . '/' . $this->dateFormatter->formatInterval($output_lifespan, 1);
151 protected function getLifespan($type) {
152 $lifespan = $this->options[$type . '_lifespan'] == 'custom' ? $this->options[$type . '_lifespan_custom'] : $this->options[$type . '_lifespan'];
156 protected function cacheExpire($type) {
157 $lifespan = $this->getLifespan($type);
159 $cutoff = REQUEST_TIME - $lifespan;
170 protected function cacheSetMaxAge($type) {
171 $lifespan = $this->getLifespan($type);
176 return Cache::PERMANENT;
183 protected function getDefaultCacheMaxAge() {
184 // The max age, unless overridden by some other piece of the rendered code
185 // is determined by the output time setting.
186 return (int) $this->cacheSetMaxAge('output');