PluginProbe
WebTotem Security / trunk
WebTotem Security vtrunk
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 trunk, at lib/Cache.php

96 lines 2.3 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 = 5; // 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 array
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 $result = [];
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 $result[$key] = true;
37 } else {
38 $result[$key] = false;
39 }
40 }
41
42 WebTotemOption::setOptions(['cache' => $cache]);
43
44 return $result;
45 }
46
47 /**
48 * Get data from cache.
49 *
50 * @param string $key
51 * Data key.
52 * @param string $host_id
53 * The data belongs to this host.
54 *
55 * @return array
56 * Returns saved data by key.
57 */
58 public static function getdata($key, $host_id) {
59
60 $cache = json_decode(WebTotemOption::getOption('cache'), true) ?: [];
61 if(array_key_exists($host_id, $cache) and
62 array_key_exists($key, $cache[$host_id])
63 ) { // and $cache[$host_id][$key]['expired'] > time()
64 return [
65 'data' => $cache[$host_id][$key]['data'],
66 'remained' => $cache[$host_id][$key]['expired'] - time(),
67 ];
68 } else {
69 return [];
70 }
71
72 }
73
74 /**
75 * Delete data from cache.
76 *
77 * @param string $key
78 * Data key.
79 * @param string $host_id
80 * The data belongs to this host.
81 *
82 * @return bool
83 */
84 public static function deleteData($key, $host_id) {
85
86 $cache = json_decode(WebTotemOption::getOption('cache'), true) ?: [];
87
88 unset($cache[$host_id][$key]);
89 WebTotemOption::setOptions(['cache' => $cache]);
90
91 return TRUE;
92
93 }
94
95 }
96