PluginProbe
Lasso Lite – Affiliate Link Manager & Product Displays / 152
Lasso Lite – Affiliate Link Manager & Product Displays v152
157 155 156 154 153 152 151 150 149 148 trunk 0.9.9 104 105 106 107 108 109 110 111 112 113 114 115 116 All 56 releases
simple-urls / classes / class-cache-per-process.php

class-cache-per-process.php in Lasso Lite – Affiliate Link Manager & Product Displays 152, at classes/class-cache-per-process.php

116 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Declare class Cache_Per_Process
4 * For cache data to Lasso Cache class's attribute and only exist on per php process
5 * Destruct function will clear all attribute data before php close
6 * Use to cache data by key then use on next step.
7 *
8 * @package Cache_Per_Process
9 */
10
11 namespace LassoLite\Classes;
12
13 /**
14 * Cache_Per_Process
15 */
16 class Cache_Per_Process {
17 /**
18 * Class instance
19 *
20 * @var null class instance
21 */
22 private static $instance = null;
23
24 /**
25 * Cache data
26 *
27 * @var array
28 */
29 protected $data = array();
30
31 /**
32 * Construction of Cache_Per_Process
33 */
34 private function __construct() {
35 }
36
37 /**
38 * Singleton design pattern - Only get object of class one time
39 *
40 * @return Cache_Per_Process
41 */
42 public static function get_instance(): self {
43 if ( null === self::$instance ) {
44 self::$instance = new self();
45 }
46
47 return self::$instance;
48 }
49
50 /**
51 * Get cache by key
52 *
53 * @param Cache key $key Cache key.
54 * @param Cache key $default Default value if cache is not exist. Default to false.
55 * @return bool|mixed
56 */
57 public function get_cache( $key, $default = false ) {
58 if ( array_key_exists( $key, $this->data ) ) {
59 return $this->data[ $key ];
60 }
61
62 return $default;
63 }
64
65 /**
66 * Set cache key and value
67 *
68 * @param Cache key $key cache key.
69 * @param Cache value $value cache value.
70 */
71 public function set_cache( $key, $value ) {
72 if ( isset( $this->data[ $key ] ) ) {
73 return;
74 }
75
76 $this->data[ $key ] = $value;
77 }
78
79 /**
80 * Unset cache by key
81 *
82 * @param Cache key $key cache key.
83 */
84 public function un_set( $key ) {
85 if ( isset( $this->data[ $key ] ) ) {
86 unset( $this->data[ $key ] );
87 }
88 }
89
90 /**
91 * Destruct cache items
92 */
93 public function __destruct() {
94 $keys = array_keys( $this->data );
95 array_walk( $keys, array( $this, 'un_set' ) );
96 }
97
98 /**
99 * Determine cache key.
100 *
101 * @param array|string $data Determine data.
102 *
103 * @return string
104 */
105 public static function determine_cache_key( $data ) {
106 return is_array( $data ) ? implode( '-', $data ) : (string) $data;
107 }
108
109 /**
110 * Clear all cache key
111 */
112 public function clear_cache() {
113 $this->data = array();
114 }
115 }
116