PluginProbe
Advanced Access Manager – Access Governance for WordPress / 7.1.2
Advanced Access Manager – Access Governance for WordPress v7.1.2
7.1.4 7.1.2 7.1.3 6.8.4 6.8.5 6.9.0 6.9.1 6.9.10 6.9.11 6.9.12 6.9.13 6.9.14 6.9.15 6.9.16 6.9.17 6.9.18 6.9.19 6.9.2 6.9.20 6.9.21 6.9.22 6.9.23 6.9.24 6.9.25 6.9.26 All 210 releases
advanced-access-manager / application / Framework / Utility / ObjectCache.php
ObjectCache.php
150 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ======================================================================
5 * LICENSE: This file is subject to the terms and conditions defined in *
6 * file 'license.txt', which is part of this source code package. *
7 * ======================================================================
8 */
9
10 /**
11 * In-memory object cache
12 *
13 * Implementing its own cache because WP core cache clones object and this causes
14 * data integrity issue between objects
15 *
16 * @package AAM
17 * @version 7.0.0
18 */
19 class AAM_Framework_Utility_ObjectCache implements AAM_Framework_Utility_Interface
20 {
21
22 use AAM_Framework_Utility_BaseTrait;
23
24 /**
25 * Default object cache capacity
26 *
27 * @version 7.0.0
28 */
29 const DEFAULT_CAPACITY = 250;
30
31 /**
32 * Internal cache
33 *
34 * @var array
35 * @access private
36 *
37 * @version 7.0.0
38 */
39 private $_cache = [];
40
41 /**
42 * @inheritDoc
43 */
44 protected function __construct() { }
45
46 /**
47 * Get cached object
48 *
49 * @param string|array $key
50 *
51 * @return mixed
52 * @access public
53 *
54 * @version 7.0.0
55 */
56 public function get($key)
57 {
58 $cache_key = $this->_prepare_cache_key($key);
59
60 return !empty($this->_cache[$cache_key]) ? $this->_cache[$cache_key] : null;
61 }
62
63 /**
64 * Set cache value
65 *
66 * @param string|array $key
67 * @param object $obj
68 *
69 * @return boolean
70 * @access public
71 *
72 * @version 7.0.0
73 */
74 public function set($key, $obj)
75 {
76 // Determine if we can use cache
77 $enabled = AAM_Framework_Manager::_()->config->get(
78 'core.settings.object_cache.enabled',
79 defined('AAM_OBJECT_CACHE_ENABLED') ? AAM_OBJECT_CACHE_ENABLED : true
80 );
81
82 if ($enabled) {
83 $cache_key = $this->_prepare_cache_key($key);
84
85 $this->_cache[$cache_key] = $obj;
86
87 if (count($this->_cache) > self::DEFAULT_CAPACITY) {
88 array_shift($this->_cache);
89 }
90 }
91
92 return true;
93 }
94
95 /**
96 * Reset cache
97 *
98 * @return bool
99 * @access public
100 *
101 * @version 7.0.0
102 */
103 public function reset()
104 {
105 $this->_cache = [];
106
107 return true;
108 }
109
110 /**
111 * Prepare cache key
112 *
113 * @param string|array $key
114 *
115 * @return string
116 * @access private
117 *
118 * @version 7.0.0
119 */
120 private function _prepare_cache_key($key)
121 {
122 // Prepare object cache key
123 if (is_array($key)) {
124 $bits = [];
125
126 foreach($key as $k) {
127 if (!empty($k)) {
128 if (is_object($k)) {
129 if (function_exists('spl_object_hash')) {
130 array_push($bits, spl_object_hash($k));
131 } else {
132 array_push($bits, serialize($k));
133 }
134 } elseif (is_scalar($k)) {
135 array_push($bits, $k);
136 } elseif (is_array($k)) {
137 array_push($bits, serialize($k));
138 }
139 }
140 }
141
142 $result = md5(implode('_', $bits));
143 } else {
144 $result = md5($key);
145 }
146
147 return $result;
148 }
149
150 }