source: trunk/prototype/app/cache/MemoryCache.php @ 5136

Revision 5136, 3.1 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2305 - Enviando alteracoes, desenvolvidas internamente na Prognus, do modulo prototype.

Line 
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 &copy; 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
30class 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    }
79
80    /**
81     * Retrieves a value from cache with an Id.
82     *
83     * @param string $id A unique key identifying the cache
84     * @return mixed The value stored in cache. Return false if no cache found or already expired.
85     */
86    public function get($id){
87     
88        $return = $this->_memcache->get($id);
89
90        if( $return ){
91            ob_start();
92            print_r( "lendo $id do cache" );
93            $output = ob_get_clean();
94            file_put_contents( "/tmp/cache.log", file_get_contents( "/tmp/cache.log" ) . $output . "\n" );
95        }
96
97        return( $return );
98    }
99   
100    /**
101     * Deletes an APC data cache with an identifying Id
102     *
103     * @param string $id Id of the cache
104     * @return bool True if success
105     */
106    public function clear($id){
107        return $this->_memcache->delete($id);
108    }
109
110    /**
111     * Deletes all data cache
112     * @return bool True if success
113     */
114    public function clearAll(){
115        return $this->_memcache->flush();
116    }
117
118}
119
Note: See TracBrowser for help on using the repository browser.