/
couchCache.php
109 lines (85 loc) · 2.68 KB
/
couchCache.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
109
<?php
/*
couchCache
==========
Ronaldo Barbachano
Dec 2011
www.doinglines.com
Simple php5 class for storing and retrieving objects with support for APC and memcached.
Eventually will support each function for APC and memcached, and perhaps varnish.
*/
class couchCache{
function __construct(){
// $this->cache= new Memcached();
// $this->cache->addServer('localhost', 11211);
}
function mc_server($servers,$action='add'){
// server:port:weight
// do sumcheck on weight to 100%
// action 1 , add, 0 remove ?
// servers should be an array, but could take a string ?
// ' servername:port servername:port severname:port '
if(is_string($servers)){
foreach(explode(' ',trim($servers)) as $server)
$a[] = explode(':',$server,3);
}
elseif(is_array($servers)){
$a = $servers;
}
return ($this->cache->addServers($a)?true : false);
}
function store($id,$data=null,$db=MEMCACHED_DB){
if($data == null){
if( $insert['d'] = $this->cache->get($id) ){
couchCurl::put(json_encode($insert),$id,$db);
return true;
// look up existing cache key (title), store to db by title as $_id
}else
return false;
}else{
foreach($data as $k=>$v){
// replacing '(' with dashes for bash could put them back...
$data[$k] = str_replace(array('(',')',',',"'"),array('-#','#-','*@','*$'),$v);
}
$insert['d'] = $data;
couchCurl::put(json_encode($insert),$id,$db);
}
}
function _apc($key,$data,$store = false){
// default to 'retrieve', returns 2 if no change
return ($store ==false && apc_exists($key) ? apc_fetch($key): (apc_exists($key) && apc_get($key) != $data ? apc_store($key, $data):2 ) );
}
function _apc_const($key,$constants=false,$case_sen=true){
// pass empty $constants array() to delete constants
if($values == false)
return apc_load_constants($key,$case_sen);
else
return apc_define_constants($key,$constants);
}
function retrieve($title,$memcached = false,$db=MEMCACHED_DB){
// look up title in db, put into memcached, to make it 'secure' add a cookie/session/random generated hash unique to the user
// to the title.
$r = json_decode(couchCurl::get($title,false,$db),true);
if(isset($r['d']) ){
//if($memcached == true ){
//$this->cache->set($title,$r['d'],time() + CMC_TIMEOUT ) ;
//}
foreach($r['d'] as $loc=>$item){
// these string replacements are mainly for the bash shell ..
$r['d'][$loc] = str_replace(array('-#','#-','*@','*$'),array('(',')',',',"'"),$item);
}
return $r['d'];
}
else
return false;
}
/*
function _apc_bin($data,$action,$resource=null){
// to do
if(in_array($action, array ('dump','dumpfile','load','loadfile')){
}else{
return false;
}
}
*/
}