PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.21
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.21
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / includes / class-wp-optimize-transients-cache.php

class-wp-optimize-transients-cache.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.21, at includes/class-wp-optimize-transients-cache.php

163 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('WPO_VERSION')) die('No direct access allowed');
4
5 /**
6 * Class WP_Optimize_Transients_Cache
7 */
8 class WP_Optimize_Transients_Cache {
9
10 private $_cache = array();
11
12 private $_expiration = array();
13
14 private $_keep_free_mem = false;
15
16 /**
17 * WP_Optimize_Transients_Cache constructor.
18 */
19 public function __construct() {
20 }
21
22 /**
23 * Return instance of WP_Optimize_Transients_Cache.
24 *
25 * @return WP_Optimize_Transients_Cache
26 */
27 public static function get_instance() {
28 static $instance;
29 if (null === $instance) {
30 $instance = new self();
31 }
32 return $instance;
33
34 }
35
36 /**
37 * Save $value to memory or to database depends on free memory.
38 *
39 * @param string $key
40 * @param mixed $value
41 * @param int $expiration
42 */
43 public function set($key, &$value, $expiration = 0) {
44 $keep_free_mem = $this->_keep_free_mem ? $this->_keep_free_mem : 16 * 1048576;
45
46 $used_memory = memory_get_usage();
47 $free_memory = WP_Optimize()->get_free_memory();
48 $released_memory = 0;
49
50 $cache_keys = array_keys($this->_cache);
51
52 // release memory while we need.
53 while ($keep_free_mem > $free_memory + $released_memory && !empty($cache_keys)) {
54 $this->flush_value(array_shift($cache_keys));
55 $released_memory = $used_memory - memory_get_usage();
56 }
57
58 // if we have enough of free memory then save to memory.
59 if (WP_Optimize()->get_free_memory() > $keep_free_mem) {
60 $this->_cache[$key] = $value;
61 $this->_expiration[$key] = $expiration;
62 } else {
63 if (isset($this->_cache[$key])) {
64 unset($this->_cache[$key]);
65 }
66
67 if (isset($this->_expiration[$key])) {
68 unset($this->_expiration[$key]);
69 }
70
71 $this->set_transient($key, $value, $expiration);
72 }
73 }
74
75 /**
76 * Return value from cache by $key.
77 *
78 * @param string $key
79 * @return mixed
80 */
81 public function get($key) {
82 if (array_key_exists($key, $this->_cache)) return $this->_cache[$key];
83
84 return $this->get_transient($key);
85 }
86
87 /**
88 * Delete value from cache.
89 *
90 * @param string $key
91 */
92 public function delete($key) {
93 if (array_key_exists($key, $this->_cache)) {
94 unset($this->_cache[$key], $this->_expiration[$key]);
95 }
96
97 $this->delete_transient($key);
98 }
99
100 /**
101 * Set transient.
102 *
103 * @param int $key
104 * @param mixed $value
105 * @param int $expiration
106 */
107 public function set_transient($key, $value, $expiration = 0) {
108 if (WP_Optimize()->is_multisite_mode()) {
109 set_site_transient($key, $value, $expiration);
110 } else {
111 set_transient($key, $value, $expiration);
112 }
113 }
114
115 /**
116 * Get transient.
117 *
118 * @param string $key
119 * @return mixed
120 */
121 public function get_transient($key) {
122 if (WP_Optimize()->is_multisite_mode()) {
123 $value = get_site_transient($key);
124 } else {
125 $value = get_transient($key);
126 }
127
128 return $value;
129 }
130
131 /**
132 * Delete transient.
133 *
134 * @param string $key
135 */
136 public function delete_transient($key) {
137 if (WP_Optimize()->is_multisite_mode()) {
138 delete_site_transient($key);
139 } else {
140 delete_transient($key);
141 }
142 }
143
144 /**
145 * Save value to database and remove from $_cache array.
146 *
147 * @param string $key
148 */
149 public function flush_value($key) {
150 $this->set_transient($key, $this->_cache[$key], $this->_expiration[$key]);
151 unset($this->_cache[$key], $this->_expiration[$key]);
152 }
153
154 /**
155 * Save all _cache values to database.
156 */
157 public function flush() {
158 foreach (array_keys($this->_cache) as $key) {
159 $this->flush_value($key);
160 }
161 }
162 }
163