PluginProbe
Leadpages / trunk
Leadpages vtrunk
1.3.0 1.2.1 1.2.2 1.2.3 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0
leadpages / includes / Cache.php

Cache.php in Leadpages trunk, at includes/Cache.php

90 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Leadpages;
4
5 defined('ABSPATH') || die('No script kiddies please!'); // Avoid direct file request
6
7
8 /**
9 * A class for managing a cache for Leadpages asset serving backed by the WP Transients API.
10 *
11 * WordPress Transients API: https://developer.wordpress.org/apis/transients/
12 * Transient expiration times are a maximum time. There is no minimum age. Transients
13 * might disappear one second after you set them, or 24 hours, but they will never be
14 * around after the expiration time.
15 *
16 * Pages are cached by the slug that they are published under in WordPress so that they
17 * can be quickly referenced when serving. The entire response when fetching the page
18 * is cached, not just the page HTML content.
19 */
20 class Cache {
21 // The maximum amount of time a value should be cached for
22 private static $max_time = 60 * 60 * 24; // 1 day
23
24 // The prefix to cache pages with
25 private static $page_key_prefix = LEADPAGES_OPT_PREFIX . '_page_';
26
27 /*
28 * Build the cache key for a page.
29 *
30 * Classic rows are keyed by slug alone (unchanged). Nova rows additionally include the platform
31 * and nova_page_id so that re-binding a slug from one Nova page to another (or across platforms)
32 * cannot serve a stale cached response.
33 *
34 * @param string $slug
35 * @param string $platform 'classic' | 'nova'
36 * @param string|null $nova_page_id
37 * @return string
38 */
39 public static function page_key( $slug, $platform = 'classic', $nova_page_id = null ) {
40 if ('nova' === $platform && $nova_page_id) {
41 return self::$page_key_prefix . 'nova_' . $nova_page_id . '_' . $slug;
42 }
43 return self::$page_key_prefix . $slug;
44 }
45
46 /*
47 * The default maximum time (seconds) a value is cached for.
48 *
49 * @return int
50 */
51 public static function default_ttl() {
52 return self::$max_time;
53 }
54
55 /*
56 * Set a value in the cache.
57 *
58 * Example:
59 * Cache::set(Cache::page_key($slug), $page)
60 *
61 * @param string $name
62 * @param mixed $value
63 * @param int|null $ttl seconds to cache for; defaults to the maximum cache time
64 * @return boolean
65 */
66 public static function set( $name, $value, $ttl = null ) {
67 return set_transient($name, $value, null === $ttl ? self::$max_time : $ttl);
68 }
69
70 /*
71 * Retrieve a value from the cache.
72 *
73 * @param string $name
74 * @return mixed
75 */
76 public static function get( $name ) {
77 return get_transient($name);
78 }
79
80 /*
81 * Remove a value from the cache.
82 *
83 * @param string $name
84 * @return boolean
85 */
86 public static function delete( $name ) {
87 return delete_transient($name);
88 }
89 }
90