| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of EC-CUBE |
|---|
| 4 | * |
|---|
| 5 | * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved. |
|---|
| 6 | * |
|---|
| 7 | * http://www.lockon.co.jp/ |
|---|
| 8 | * |
|---|
| 9 | * This program is free software; you can redistribute it and/or |
|---|
| 10 | * modify it under the terms of the GNU General Public License |
|---|
| 11 | * as published by the Free Software Foundation; either version 2 |
|---|
| 12 | * of the License, or (at your option) any later version. |
|---|
| 13 | * |
|---|
| 14 | * This program is distributed in the hope that it will be useful, |
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | * GNU General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU General Public License |
|---|
| 20 | * along with this program; if not, write to the Free Software |
|---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * Cache controll using PEAR::Cache_Lite. |
|---|
| 26 | */ |
|---|
| 27 | class SC_Cache |
|---|
| 28 | { |
|---|
| 29 | /** |
|---|
| 30 | * Instance of PEAR::Cache_Lite class. |
|---|
| 31 | * @var object |
|---|
| 32 | */ |
|---|
| 33 | public static $_instance = NULL; |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * Default cache lifetime. |
|---|
| 37 | */ |
|---|
| 38 | const LIFETIME = MAX_LIFETIME; |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Directory to save cache files. |
|---|
| 42 | */ |
|---|
| 43 | const CACHEDIR = MASTER_DATA_REALDIR; |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * Create Cache_Lite object and set it to static variable. |
|---|
| 47 | * |
|---|
| 48 | * @return void |
|---|
| 49 | */ |
|---|
| 50 | public static function forge() |
|---|
| 51 | { |
|---|
| 52 | $options = array( |
|---|
| 53 | 'cacheDir' => SC_Cache_Ex::CACHEDIR, |
|---|
| 54 | 'lifeTime' => SC_Cache_Ex::LIFETIME, |
|---|
| 55 | 'automaticSerialization' => TRUE |
|---|
| 56 | ); |
|---|
| 57 | SC_Cache_Ex::$_instance = new Cache_Lite($options); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * Get Cache_Lite object. |
|---|
| 62 | * |
|---|
| 63 | * @return void |
|---|
| 64 | */ |
|---|
| 65 | public static function getInstance() |
|---|
| 66 | { |
|---|
| 67 | is_null(SC_Cache_Ex::$_instance) and SC_Cache_Ex::forge(); |
|---|
| 68 | |
|---|
| 69 | return SC_Cache_Ex::$_instance; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * Get data from cache. |
|---|
| 74 | * |
|---|
| 75 | * @param string $id cache id |
|---|
| 76 | * @param string $group name of the cache group |
|---|
| 77 | * @param int $lifeTime custom lifetime |
|---|
| 78 | * @return mixed data of cache (else : false) |
|---|
| 79 | */ |
|---|
| 80 | public static function get($id, $group = 'default', $lifeTime = NULL) |
|---|
| 81 | { |
|---|
| 82 | $processor = SC_Cache_Ex::getInstance(); |
|---|
| 83 | |
|---|
| 84 | // set custom lifetime. |
|---|
| 85 | !is_null($lifeTime) and $processor->setOption('lifeTime', $lifeTime); |
|---|
| 86 | |
|---|
| 87 | $cache = $processor->get($id, $group); |
|---|
| 88 | |
|---|
| 89 | // set back to default lifetime. |
|---|
| 90 | !is_null($lifeTime) and $processor->setOption('lifeTime', SC_Cache_Ex::$_lifetime); |
|---|
| 91 | |
|---|
| 92 | return $cache; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * Save data into cache. |
|---|
| 97 | * |
|---|
| 98 | * @param mixed $data data of cache |
|---|
| 99 | * @param string $id cache id |
|---|
| 100 | * @param string $group name of the cache group |
|---|
| 101 | * @return void |
|---|
| 102 | */ |
|---|
| 103 | public static function save($data, $id, $group = 'default') |
|---|
| 104 | { |
|---|
| 105 | $processor = SC_Cache_Ex::getInstance(); |
|---|
| 106 | |
|---|
| 107 | $processor->save($data, $id, $group); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | * Clean cache. |
|---|
| 112 | * |
|---|
| 113 | * @param string $group name of the cache group |
|---|
| 114 | * @return void |
|---|
| 115 | */ |
|---|
| 116 | public static function clean($group = FALSE) |
|---|
| 117 | { |
|---|
| 118 | $processor = SC_Cache_Ex::getInstance(); |
|---|
| 119 | |
|---|
| 120 | $processor->clean($group); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|