PluginProbe
WebTotem Security / 2.4.32
WebTotem Security v2.4.32
3.0.1 3.0.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.1 2.2.2 2.2.3 2.2.4 All 109 releases
wt-security / lib / Cache.php

Cache.php in WebTotem Security 2.4.32, at lib/Cache.php

93 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 if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) {
4 if (!headers_sent()) {
5 header('HTTP/1.1 403 Forbidden');
6 }
7 die("Protected By WebTotem!");
8 }
9 /**
10 * WebTotem Cache class for Wordpress.
11 */
12 class WebTotemCache {
13
14 const WTOTEM_CACHE_STORAGE_TIME = 3; // cache storage time in minutes
15 /**
16 * Save multiple some data to cache.
17 *
18 * @param array $data
19 * Array of data.
20 * @param string $host_id
21 * The data belongs to this host.
22 * @param string $storage_time
23 * Cache storage time in minutes.
24 *
25 * @return bool
26 * Returns TRUE after saving the data.
27 */
28 public static function setData(array $data, $host_id, $storage_time = self::WTOTEM_CACHE_STORAGE_TIME) {
29
30 $cache = json_decode(WebTotemOption::getOption('cache'), true) ?: [];
31
32 foreach ($data as $key => $value){
33 if(!empty($value) and !array_key_exists('errors', $value)){
34 $expired = time() + ( $storage_time * 60 );
35 $cache[$host_id][$key] = ['data' => $value, 'expired' => $expired];
36 }
37 }
38
39 WebTotemOption::setOptions(['cache' => $cache]);
40
41 return TRUE;
42 }
43
44 /**
45 * Get data from cache.
46 *
47 * @param string $key
48 * Data key.
49 * @param string $host_id
50 * The data belongs to this host.
51 *
52 * @return array
53 * Returns saved data by key.
54 */
55 public static function getdata($key, $host_id) {
56
57 $cache = json_decode(WebTotemOption::getOption('cache'), true) ?: [];
58 if(array_key_exists($host_id, $cache) and
59 array_key_exists($key, $cache[$host_id]) and
60 $cache[$host_id][$key]['expired'] > time()) {
61 return [
62 'data' => $cache[$host_id][$key]['data'],
63 'remained' => $cache[$host_id][$key]['expired'] - time(),
64 ];
65 } else {
66 return [];
67 }
68
69 }
70
71 /**
72 * Delete data from cache.
73 *
74 * @param string $key
75 * Data key.
76 * @param string $host_id
77 * The data belongs to this host.
78 *
79 * @return bool
80 */
81 public static function deleteData($key, $host_id) {
82
83 $cache = json_decode(WebTotemOption::getOption('cache'), true) ?: [];
84
85 unset($cache[$host_id][$key]);
86 WebTotemOption::setOptions(['cache' => $cache]);
87
88 return TRUE;
89
90 }
91
92 }
93