1 | <?php |
---|
2 | /** |
---|
3 | * DooMemCache class file. |
---|
4 | * |
---|
5 | * @author Leng Sheng Hong <darkredz@gmail.com> |
---|
6 | * @link http://www.doophp.com/ |
---|
7 | * @copyright Copyright © 2009 Leng Sheng Hong |
---|
8 | * @license http://www.doophp.com/license |
---|
9 | */ |
---|
10 | |
---|
11 | |
---|
12 | /** |
---|
13 | * DooMemCache provides caching methods utilizing the Memcache extension. |
---|
14 | * |
---|
15 | * If you have multiple servers for memcache, you would have to set it up in common.conf.php |
---|
16 | * <code> |
---|
17 | * // host, port, persistent, weight |
---|
18 | * $config['MEMCACHE'] = array( |
---|
19 | * array('192.168.1.31', '11211', true, 40), |
---|
20 | * array('192.168.1.23', '11211', true, 80) |
---|
21 | * ); |
---|
22 | * </code> |
---|
23 | * |
---|
24 | * @author Leng Sheng Hong <darkredz@gmail.com> |
---|
25 | * @version $Id: DooMemCache.php 1000 2009-08-22 19:36:10 |
---|
26 | * @package doo.cache |
---|
27 | * @since 1.1 |
---|
28 | */ |
---|
29 | |
---|
30 | class MemoryCache{ |
---|
31 | /** |
---|
32 | * Memcached connection |
---|
33 | * @var Memcache |
---|
34 | */ |
---|
35 | protected $_memcache; |
---|
36 | |
---|
37 | /** |
---|
38 | * Configurations of the connections |
---|
39 | * @var array |
---|
40 | */ |
---|
41 | protected $_config; |
---|
42 | |
---|
43 | public function __construct($conf=Null) { |
---|
44 | $this->_memcache = new Memcache(); |
---|
45 | $this->_config = $conf; |
---|
46 | |
---|
47 | // host, port, persistent, weight |
---|
48 | if($conf!==Null){ |
---|
49 | foreach ($conf as $c){ |
---|
50 | $result = $this->_memcache->addServer($c[0], $c[1], $c[2], $c[3]); |
---|
51 | } |
---|
52 | } |
---|
53 | else{ |
---|
54 | $this->_memcache->addServer('localhost', 11211); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Adds a cache with an unique Id. |
---|
60 | * |
---|
61 | * @param string $id Cache Id |
---|
62 | * @param mixed $data Data to be stored |
---|
63 | * @param int $expire Seconds to expired |
---|
64 | * @param int $compressed To store the data in Zlib compressed format |
---|
65 | * @return bool True if success |
---|
66 | */ |
---|
67 | public function put($id, $data, $expire=0, $compressed=false){ |
---|
68 | |
---|
69 | ob_start(); |
---|
70 | print_r( "criando $id e armazenando no cache" ); |
---|
71 | $output = ob_get_clean(); |
---|
72 | file_put_contents( "/tmp/cache.log", file_get_contents( "/tmp/cache.log" ) . $output . "\n" ); |
---|
73 | |
---|
74 | // if($compressed) |
---|
75 | // return $this->_memcache->set($id, $data, MEMCACHE_COMPRESSED, $expire); |
---|
76 | // else |
---|
77 | // return $this->_memcache->set($id, $data, 0, $expire); |
---|
78 | return false; |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Retrieves a value from cache with an Id. |
---|
83 | * |
---|
84 | * @param string $id A unique key identifying the cache |
---|
85 | * @return mixed The value stored in cache. Return false if no cache found or already expired. |
---|
86 | */ |
---|
87 | public function get($id){ |
---|
88 | |
---|
89 | // $return = $this->_memcache->get($id); |
---|
90 | $return = false; |
---|
91 | |
---|
92 | if( $return ){ |
---|
93 | ob_start(); |
---|
94 | print_r( "lendo $id do cache" ); |
---|
95 | $output = ob_get_clean(); |
---|
96 | file_put_contents( "/tmp/cache.log", file_get_contents( "/tmp/cache.log" ) . $output . "\n" ); |
---|
97 | } |
---|
98 | |
---|
99 | return( $return ); |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * Deletes an APC data cache with an identifying Id |
---|
104 | * |
---|
105 | * @param string $id Id of the cache |
---|
106 | * @return bool True if success |
---|
107 | */ |
---|
108 | public function clear($id){ |
---|
109 | return $this->_memcache->delete($id); |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * Deletes all data cache |
---|
114 | * @return bool True if success |
---|
115 | */ |
---|
116 | public function clearAll(){ |
---|
117 | return $this->_memcache->flush(); |
---|
118 | } |
---|
119 | |
---|
120 | } |
---|
121 | |
---|