root/lang/php/Cache/Resource/MemCache.php @ 1523

Revision 1523, 9.2 kB (checked in by takuya, 6 years ago)

ローカルにあるライブラリをUP

Line 
1<?php
2
3/**
4 *  Save cache data by memcached
5 * @package     Cache_Handler
6 * @category    Cache
7 */
8
9class  Cache_Handler_Resource_MemCache implements Cache_Handler_Resource_Interface {
10    public $memcache;
11    /**
12    * constructor for php4 compatible
13    * @access public
14    * @param  $ini array of string for config.
15    *         ini array is like this ,array( 'host'=>'localhost', 'port'=> 11211 )
16    */
17    public function __construct( $ini )
18    {
19        $this->ttl = 3600;//default time out.
20        foreach( $ini as $key => $val ){
21            $this->$key = $val;
22        }
23        $this->memcache = new MemCache();
24        $this->memcache->connect( $this->host, $this->port );
25    }
26   
27    /**
28    * wrapper
29    * @access private
30    */
31    public function _insert( $id, $data, $ttl )
32    {
33        $this->memcache->delete( $id );
34        $ret = $this->memcache->set( $id, $data, 0, $ttl );
35        return $ret;
36    }
37    /**
38    * wrapper
39    * @access private
40    */
41    public function _delete( $id )
42    {
43        return $this->memcache->delete( $id );
44    }
45    /**
46    * Call by hander
47    * @access public
48    * @param $data  object cache data
49    * @param $id    stirng cache id
50    * @param $group array  Array of String. group name of this cache
51    * @return boolean  true (succeeded) / false (faild)
52    */
53    public function save$data, $id, $group = null )
54    {
55        $ret[]= $this->_insert( $id, $data, $this->ttl );
56        $ret[]= $this->addGroup( $id, $group );
57        $ret[]= $this->setTimeToLive( $id, $this->ttl );
58        $ret[]= $this->setLastModified( $id, date("r") );
59        return ($ret[0] && $ret[1] && $ret[2] && $ret[3] );
60    }
61    /**
62    * Call by hander
63    * definition of remove method
64    * @access public
65    * @param $id    stirng cache id
66    * @return boolean  true (succeeded) / false (faild)
67    */
68    public function remove( $id )
69    {
70        $ret[] = $this->_delete( $id );
71        $ret[] = $this->_leaveGroup( $id );
72        $ret[] = $this->_deleteLastModified( $id );
73        $ret[] = $this->_deleteTTL( $id );
74        return ( $ret[0] && $ret[1] && $ret[2] && $ret[3] );
75    }
76    /**
77    * Call by hander
78    * definition of clean method
79    * without group name, clean all chache
80    * @access public
81    * @param  String $group    stirng cache group name
82    * @param  Array  $ids array of id removed.
83    * @return boolean  true (succeeded) / false (faild)
84    */
85    public function clean( $group =null, &$ids = null )
86    {
87        if( $group == null ){
88            return $this->_cleanAll();
89        }else{
90            return $this->_removeGroup( $group, &$ids );
91        }
92    }
93    public function _cleanAll(){
94        return $this->memcache->flush();
95    }
96    /**
97    * Call by hander
98    * definition of get method
99    * get Cache data from this resource
100    * @access public
101    * @param $id    stirng cache id
102    * @return string cached data, if cache does not exist, returns null
103    */
104    public function get( $id )
105    {
106        $content = $this->memcache->get( $id );
107        if( !$content === false ){
108            return $content;
109        }else{
110            return null;
111        }
112    }
113    public function _getModifiedKeyName()
114    {
115        return ".modified";
116    }
117    public function _deleteLastModified( $id )
118    {
119        return $this->_delete( $id.$this->_getModifiedKeyName() );
120    }
121    /**
122    * Call by hander.
123    * get Cache created time from this resource.
124    * @access public
125    * @param $id    stirng cache id
126    * @return string cache created time
127    */
128    public function getLastModified( $id )
129    {
130        return $this->get( $id.$this->_getModifiedKeyName() );
131    }
132    /**
133    * Call by hander.
134    * Definition of getLastModified method.
135    * Set or update  Cache created(modified) time.
136    * @access public
137    * @param $id    stirng cache id
138    * @param $date  stirng cache created time. default value date("r")
139    * @return String date of cache create or modified time
140    */
141    public function setLastModified( $id, $date )
142    {
143        if( $date == null){
144            $date = date("r");
145        }
146        return $this->_insert( $id.$this->_getModifiedKeyName(),
147                               $date, $this->ttl
148                             );
149    }
150    /**
151    * Call by hander.
152    * definition of setTimeToLive method.
153    * set or update cache TTL for checking expiration.
154    * @access public
155    * @param $id    stirng cache id
156    * @param $int   int    cache lifetime , default -1
157    * @return boolean  true (succeeded) / false (faild)
158    */
159    public function setTimeToLive( $id, $int = null )
160    {
161        if( $int == null ){
162            $int = $this->ttl;
163        }else if( $int != $this->ttl ){
164            $this->ttl = $int;
165            $this->memcache->delete( $id, $this->ttl );
166        }
167        return $this->_insert( $id.$this->_getTTLKeyName(),
168                               $this->ttl,
169                               $this->ttl
170                             );
171    }
172    /**
173    * Call by hander.
174    * definition of getTimeToLive method.
175    * get cache Life time for checking expiration.
176    * @access public
177    * @param $id    stirng cache id
178    * @return int   cache lifetime. if value does not exits , this method return -1.
179    */
180    public function getTimeToLive( $id )
181    {
182        return $this->get( $id.$this->_getTTLKeyName() );
183    }
184    public function _getTTLKeyName()
185    {
186        return ".ttl";
187    }
188    public function _deleteTTL( $id )
189    {
190        return $this->_delete( $id.$this->_getTTLKeyName() );
191    }
192    public function getGroupList()
193    {
194        $ret = $this->get( $this->getGroupKeyName() );
195        if( $ret == null ){
196            return array();
197        }else{
198            return unserialize( $ret );
199        }
200    }
201    public function getGroupKeyName()
202    {
203        return md5(".cache.groups");
204    }
205    //array( array( "id" => "group name" ) )
206    public function saveGroupList( $array )
207    {
208        $str = serialize( $array );
209        return $this->memcache->set( $groupname.$this->getGroupKeyName(), $str );
210    }
211    /**
212    * Call by hander.
213    * Add $id to cache group $group
214    * @access public
215    * @param $id    stirng cache id
216    * @param $group mixed  stirng/array cache group name
217    * @return boolean  true (succeeded) / false (faild)
218    */
219    public function addIntoGroup( $id, $group ){
220        return $this->addGroup( $id, $group );
221    }
222
223    public function addGroup( $id, $groupnames )
224    {
225        if( $groupnames == null || $groupnames == "" ){
226            return true;
227        }
228        if( !is_array( $groupnames ) ){
229            $groupnames = array( $groupnames );
230        }
231        $list = $this->getGroupList();
232        foreach( $groupnames as $groupname ){
233            if( sizeof( $list ) == 0 ){
234                $list[] = array( $id => $groupname );
235                continue;
236            }
237            foreach( $list as $pair ){
238                list( $_key, $_val ) = each( $pair );
239                if( $_key == $id && $_val == $groupname ){
240                    continue;
241                }else{
242                    $list[] = array( $id => $groupname );
243                }
244            }
245        }
246        return $this->saveGroupList( $list );
247    }
248    public function _removeGroup( $groupname, &$ids = null )
249    {
250        $list = $this->getGroupList();
251        foreach( $list as $idx => $pair ){
252            list( $_key, $_val ) = each( $pair );
253            if( $_val == $groupname ){
254                $ids[] = $_key;
255                unset( $list[$idx] );
256            }
257        }
258        $list  = array_values( $list );
259        $ret[] = $this->saveGroupList( $list );
260        foreach( $ids as $id ){
261            $ret[] = $this->remove( $id );
262        }
263        return !in_array( $ret, false, true );//if $ret has false, return false;
264    }
265    /**
266    * remove cache id ( arg $id ) from group index
267    * @access public
268    * @param  String $id cache id
269    * @return boolean  true (succeeded) / false (faild)
270    */
271    public function removeFromGroup( $id, $group = null )
272    {
273        $ret[] = $this->remove( $id );
274        $ret[] = $this->_leaveGroup( $id, $group );
275        return !in_array( $ret, false, true );//if $ret has false, return false;
276    }
277    /**
278    * remove cache id ( arg $id ) from group index
279    * @access public
280    * @param  String $id cache id
281    * @param  String $group Cache group name
282    * @return boolean  true (succeeded) / false (faild)
283    */
284    public function _leaveGroup( $id, $group = null )
285    {
286        $list = $this->getGroupList();
287        foreach( $list as $idx => $pair ){
288            list( $_key, $_val ) = each( $pair );
289            if( $_key == $id ){
290                if( $group == null || $group == $_val ){
291                    $ids[] = $_key;
292                    unset( $list[$idx] );
293                }
294            }
295        }
296        $list  = array_values( $list );
297        $ret[] = $this->saveGroupList( $list );
298        return $ret[0] && $ret[1];
299   
300    }
301}
Note: See TracBrowser for help on using the browser.