source: branches/version-2_13-dev/data/class/SC_Cache.php @ 23141

Revision 23141, 3.2 KB checked in by m_uehara, 7 years ago (diff)

#2275
影響が大きいため、2.13.0では対応を行わない r23125 r23128 r23133 を差し戻します。

Line 
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
24require DATA_REALDIR . 'module/Cache/Lite.php';
25
26/**
27 * Cache controll using PEAR::Cache_Lite.
28 */
29class SC_Cache
30{
31    /**
32     * Instance of PEAR::Cache_Lite class.
33     * @var object
34     */
35    public static $_instance = NULL;
36
37    /**
38     * Default cache lifetime.
39     */
40    const LIFETIME = MAX_LIFETIME;
41
42    /**
43     * Directory to save cache files.
44     */
45    const CACHEDIR = MASTER_DATA_REALDIR;
46
47    /**
48     * Create Cache_Lite object and set it to static variable.
49     *
50     * @return void
51     */
52    public static function forge()
53    {
54        $options = array(
55            'cacheDir' => SC_Cache_Ex::CACHEDIR,
56            'lifeTime' => SC_Cache_Ex::LIFETIME,
57            'automaticSerialization' => TRUE
58        );
59        SC_Cache_Ex::$_instance = new Cache_Lite($options);
60    }
61
62    /**
63     * Get Cache_Lite object.
64     *
65     * @return void
66     */
67    public static function getInstance()
68    {
69        is_null(SC_Cache_Ex::$_instance) and SC_Cache_Ex::forge();
70
71        return SC_Cache_Ex::$_instance;
72    }
73
74    /**
75     * Get data from cache.
76     *
77     * @param  string $id       cache id
78     * @param  string $group    name of the cache group
79     * @param  int    $lifeTime custom lifetime
80     * @return mixed  data of cache (else : false)
81     */
82    public static function get($id, $group = 'default', $lifeTime = NULL)
83    {
84        $processor = SC_Cache_Ex::getInstance();
85
86        // set custom lifetime.
87        !is_null($lifeTime) and $processor->setOption('lifeTime', $lifeTime);
88
89        $cache = $processor->get($id, $group);
90
91        // set back to default lifetime.
92        !is_null($lifeTime) and $processor->setOption('lifeTime', SC_Cache_Ex::$_lifetime);
93
94        return $cache;
95    }
96
97    /**
98     * Save data into cache.
99     *
100     * @param  mixed  $data  data of cache
101     * @param  string $id    cache id
102     * @param  string $group name of the cache group
103     * @return void
104     */
105    public static function save($data, $id, $group = 'default')
106    {
107        $processor = SC_Cache_Ex::getInstance();
108
109        $processor->save($data, $id, $group);
110    }
111
112    /**
113     * Clean cache.
114     *
115     * @param  string $group name of the cache group
116     * @return void
117     */
118    public static function clean($group = FALSE)
119    {
120        $processor = SC_Cache_Ex::getInstance();
121
122        $processor->clean($group);
123    }
124}
Note: See TracBrowser for help on using the repository browser.