PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 All 140 releases
learnpress / inc / Databases / class-thim-cache-db.php

class-thim-cache-db.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.9, at inc/Databases/class-thim-cache-db.php

164 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || exit();
3
4 if ( class_exists( 'Thim_Cache_DB' ) ) {
5 return;
6 }
7
8 /**
9 * Class Thim_Cache_DB
10 *
11 * @author tungnx
12 * @version 1.0.1
13 * @since 4.2.2
14 */
15 class Thim_Cache_DB {
16 private static $_instance = null;
17 private $action = null; // one of insert/update
18 const ACTION_INSERT = 'insert';
19 const ACTION_UPDATE = 'update';
20
21 /**
22 * Set action for thim cache (one of insert/update)
23 * Default is null
24 *
25 * @param string|null $action
26 * @description Null for not set manual action insert/update
27 * @description Else it will be auto check exist key_cache to insert/update
28 * @return Thim_Cache_DB
29 */
30 public function set_action( $action ): Thim_Cache_DB {
31 $this->action = $action;
32 return $this;
33 }
34
35 /**
36 * Singleton
37 */
38 public static function instance(): Thim_Cache_DB {
39 if ( is_null( self::$_instance ) ) {
40 self::$_instance = new static();
41 }
42
43 return self::$_instance;
44 }
45
46 /**
47 * @var wpdb $wpdb
48 */
49 public $wpdb;
50 public $table_name;
51
52 protected function __construct() {
53 $this->wpdb = $GLOBALS['wpdb'];
54 $this->table_name = $this->wpdb->prefix . 'thim_cache';
55 }
56
57 /**
58 * Get value by key_cache
59 *
60 * @param string $key_cache
61 *
62 * @return bool|string
63 */
64 public function get_value( string $key_cache ) {
65 $sql = $this->wpdb->prepare(
66 "SELECT `value`, `expiration` FROM {$this->table_name} WHERE `key_cache` = %s",
67 $key_cache
68 );
69
70 $result = $this->wpdb->get_row( $sql );
71 if ( is_null( $result ) ) {
72 return false;
73 }
74
75 $expiration = (int) ( $result->expiration ?? 0 );
76 if ( $expiration > 0 && $expiration < time() ) {
77 try {
78 $this->remove_cache( $key_cache );
79 } catch ( Throwable $e ) {
80 LP_Debug::error_log( $e );
81 }
82 return false;
83 }
84
85 return $result->value;
86 }
87
88 /**
89 * Get value by key_cache
90 *
91 * @param string $key_cache
92 * @param string $value
93 * @param int $expire timestamp
94 * @return bool|int|mysqli_result|resource|null
95 */
96 public function set_value( string $key_cache, string $value, int $expire = 0 ) {
97 $action = self::ACTION_INSERT;
98
99 // Auto check exist key_cache
100 $value_old = $this->get_value( $key_cache );
101 if ( false !== $value_old ) {
102 $action = self::ACTION_UPDATE;
103 }
104
105 if ( self::ACTION_UPDATE === $action ) { // Update
106 $sql = $this->wpdb->prepare(
107 "UPDATE {$this->table_name} SET value = %s, expiration = %d WHERE key_cache = %s",
108 $value,
109 $expire,
110 $key_cache
111 );
112 } else { // Insert
113 $sql = $this->wpdb->prepare(
114 "INSERT INTO {$this->table_name} (key_cache, value, expiration) VALUES (%s, %s, %d)",
115 $key_cache,
116 $value,
117 $expire
118 );
119 }
120
121 return $this->wpdb->query( $sql );
122 }
123
124 /**
125 * Delete value by key_cache
126 *
127 * @param string $key_cache
128 *
129 * @return bool|int|mysqli_result|resource|null
130 * @throws Exception
131 */
132 public function remove_cache( string $key_cache ) {
133 $sql = $this->wpdb->prepare(
134 "DELETE FROM {$this->table_name} WHERE key_cache = %s",
135 $key_cache
136 );
137
138 $result = $this->wpdb->query( $sql );
139
140 if ( $this->wpdb->last_error ) {
141 throw new Exception( $this->wpdb->last_error );
142 }
143
144 return $result;
145 }
146
147 /**
148 * Delete all cache.
149 *
150 * @return bool|int|mysqli_result|resource|null
151 * @throws Exception
152 */
153 public function remove_all_cache() {
154 $sql = "TRUNCATE TABLE {$this->table_name}";
155 $result = $this->wpdb->query( $sql );
156
157 if ( $this->wpdb->last_error ) {
158 throw new Exception( $this->wpdb->last_error );
159 }
160
161 return $result;
162 }
163 }
164