-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemcached.cache.class.php
108 lines (91 loc) · 2.6 KB
/
memcached.cache.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* iDB Memcached Cache Class
*
* Version: 1.12
* Started: 19-03-2015
* Updated: 29-11-2023
*
*/
spl_autoload_register(function($class){
require_once __DIR__ . "/cache.class.php";
});
/**
* Database disk cache
*
*/
class iDB_Cache extends idb_Cache_Core {
private static $connected = false;
private $memcached = null;
private $host = 'localhost';
private $port = 11211;
private $compress = 0;
private $expiry = 3600;
/**
* Behaves as a contructor
* And overwrites the init of the idb_Cache_Core class
*/
protected function init(){
if (defined('DB_MEMCACHED_HOST'))
$this->host = DB_MEMCACHED_HOST;
if (defined('DB_MEMCACHED_PORT'))
$this->port = DB_MEMCACHED_PORT;
if (defined('DB_MEMCACHED_COMPRESS'))
$this->compress = DB_MEMCACHED_COMPRESS;
if (defined('DB_MEMCACHED_DEFAULT_EXPIRY'))
$this->expiry = DB_MEMCACHED_DEFAULT_EXPIRY;
}
/**
* Clears all Memcached
*
* @since 1.12
* @return true on success or false on failure.
*/
public function flush() {
if(!$this->connect()) {
return false;
}
return $this->memcached->flush();
}
public function delete($key, $timeout = 0) {
if(!$this->connect() || empty($key))
return false;
return $this->memcached->delete($key, $timeout);
}
/**
*
* @return mixed Database query results
*/
public function set($key, $value, $ttl=NULL) {
if(!$this->connect())
return false;
$expiry = !$ttl ? $this->expiry : $ttl;
$this->memcached->set($key, $value, (int)$expiry);
return true;
}
/**
* Gets the cached results
*
* @param string $query SQL query.
*
* @return mixed Database query results
*/
public function get($key) {
if(!$this->connect() || empty($key))
return null;
return $this->memcached->get($key);
}
private function connect() {
if(self::$connected === true && !empty($this->memcached))
return true;
if(class_exists('Memcached')) {
$this->memcached = new Memcached;
if($this->memcached->addServer($this->host, $this->port))
return self::$connected = true;
$this->show_errors ? trigger_error("Could not connect to memcache server", E_USER_WARNING) : null;
return false;
}
$this->show_errors ? trigger_error("No memcache client exists", E_USER_WARNING) : null;
return false;
}
}