PluginProbe
wpForo Forum / 3.0.8
wpForo Forum v3.0.8
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / classes / RamCache.php

RamCache.php in wpForo Forum 3.0.8, at classes/RamCache.php

102 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace wpforo\classes;
4
5 // Exit if accessed directly
6 if( ! defined( 'ABSPATH' ) ) exit;
7
8 class RamCache {
9 /**
10 * @var array
11 */
12 private static $ram_cache = [];
13
14 /**
15 * wpForoRamCache constructor.
16 */
17 public function __construct() {
18 $this->reset();
19 }
20
21 /**
22 * set empty array to static $ram_cache
23 *
24 * @param mixed $key
25 *
26 * @return void
27 */
28 public function reset( $key = null ) {
29 if( is_null( $key ) ) {
30 self::$ram_cache = [];
31 } else {
32 unset( self::$ram_cache[ $this->fix_key( $key ) ] );
33 }
34 }
35
36 /**
37 * @param mixed $key
38 *
39 * @return string
40 */
41 private function fix_key( $key ) {
42 if( ! is_scalar( $key ) ) $key = wp_json_encode( $key );
43
44 return md5( $key );
45 }
46
47 /**
48 * checking if this data already cached
49 *
50 * @param mixed $key unique key
51 *
52 * @return bool
53 */
54 public function exists( $key ) {
55 return array_key_exists( $this->fix_key( $key ), self::$ram_cache );
56 }
57
58 /**
59 * return already cached data
60 *
61 * @param mixed $key unique key
62 *
63 * @return mixed
64 */
65 public function get( $key ) {
66 if( $this->exists( $key ) ) {
67 return self::$ram_cache[ $this->fix_key( $key ) ];
68 } else {
69 return null;
70 }
71 }
72
73 /**
74 * storing a cache of provided data
75 *
76 * @param mixed $key unique key
77 * @param mixed $data
78 */
79 public function set( $key, $data ) {
80 self::$ram_cache[ $this->fix_key( $key ) ] = $data;
81 }
82
83 /**
84 * call callable function and return function returned value
85 * and store in static property for next call
86 *
87 * @param callable $func
88 * @param mixed ...$args [optional] call_user_func parameters
89 *
90 * @return mixed
91 */
92 public function call_user_func( $func, ...$args ) {
93 if( ! is_callable( $func, false, $callable_name ) ) return null;
94 $key = [ $callable_name, $args ];
95 if( $this->exists( $key ) ) return $this->get( $key );
96 $data = call_user_func_array( $func, $args );
97 $this->set( $key, $data );
98
99 return $data;
100 }
101 }
102