Code Duplication    Length = 159-160 lines in 2 locations

code/api/EcommerceCache.php 1 location

@@ 7-166 (lines=160) @@
4
 *
5
 */
6
7
class EcommerceCache extends Object implements flushable
8
{
9
    private static $cache_in_mysql_tables = array(
10
    );
11
12
    public static function flush()
13
    {
14
        $cache = SS_Cache::factory('any');
15
        $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
16
        $tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables');
17
        if (is_array($tables)) {
18
            foreach ($tables as $table) {
19
                $table = self::make_mysql_table_name($table);
20
                DB::query(
21
                    '
22
                    DROP TABLE IF EXISTS "'.$table.'";
23
                    '
24
                );
25
                DB::query(
26
                    '
27
                    CREATE TABLE "'.$table.'" (
28
                      "PAGEID" int(11) NOT NULL,
29
                      "CACHEKEY" CHAR(50) NOT NULL,
30
                      "DATA" TEXT
31
                    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
32
                    '
33
                );
34
35
                DB::query(
36
                    '
37
                    ALTER TABLE "'.$table.'"
38
                      ADD PRIMARY KEY ("PAGEID","CACHEKEY"),
39
                      ADD KEY "CACHEKEY" ("CACHEKEY"),
40
                      ADD KEY "PAGEID" ("PAGEID");
41
                    '
42
                );
43
            }
44
        }
45
    }
46
47
    public static function clean()
48
    {
49
        $tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables');
50
        if (is_array($tables)) {
51
            foreach ($tables as $table) {
52
                $table = self::make_mysql_table_name($table);
53
                DB::query(
54
                    '
55
                    DELETE FROM "'.$table.'";
56
                    '
57
                );
58
            }
59
        }
60
    }
61
62
    private static $_items = array();
63
64
    /**
65
     * @param string           $table
66
     * @param int              $id
67
     * @param string           $cacheKey
68
     * @return mixed
69
     */
70
    public static function load($table, $id, $cacheKey)
71
    {
72
        $tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables');
73
        if (is_array($tables) && in_array($table, $tables)) {
74
            $table = self::make_mysql_table_name($table);
75
            $id = (int)$id;
76
            if (isset(self::$_items[$table])) {
77
                if (isset(self::$_items[$table][$id])) {
78
                    if (isset(self::$_items[$table][$id][$cacheKey])) {
79
                        if (self::$_items[$table][$id][$cacheKey] !== null) {
80
                            return @unserialize(self::$_items[$table][$id][$cacheKey]);
81
                        }
82
                    }
83
                }
84
            }
85
86
            //we are now loading the data ...
87
            if (!isset(self::$_items[$table])) {
88
                self::$_items[$table] = array();
89
            }
90
            if (! isset(self::$_items[$table][$id])) {
91
                self::$_items[$table][$id] = array();
92
                $rows = DB::query('SELECT "CACHEKEY", "DATA" FROM "'.$table.'" WHERE "PAGEID" = '.$id.' ;');
93
                foreach ($rows as $row) {
94
                    self::$_items[$table][$id][$row['CACHEKEY']] = $row['DATA'];
95
                }
96
                //return the value, if there is one.
97
                if (isset(self::$_items[$table][$id])) {
98
                    if (isset(self::$_items[$table][$id][$cacheKey])) {
99
                        return @unserialize(self::$_items[$table][$id][$cacheKey]);
100
                    }
101
                }
102
            }
103
        } else {
104
            $cache = SS_Cache::factory($table.'_'.$id);
105
            $data = $cache->load($cacheKey);
106
            if (!$data) {
107
                return;
108
            }
109
110
            return @unserialize($data);
111
        }
112
    }
113
114
    /**
115
     * @param string           $table
116
     * @param int              $id
117
     * @param string           $cacheKey
118
     * @param mixed            $data
119
     *
120
     */
121
    public static function save($table, $id, $cacheKey, $data)
122
    {
123
        $tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables');
124
        if (is_array($tables) && in_array($table, $tables)) {
125
            $table = self::make_mysql_table_name($table);
126
            $id = (int)$id;
127
            if (strlen($cacheKey) > 50) {
128
                user_error('ERROR: CACHEKEY longer than 50 characters: '.$cacheKey);
129
            }
130
            $data = Convert::raw2sql(serialize($data));
131
            if (!isset(self::$_items[$table])) {
132
                self::$_items[$table] = array();
133
            }
134
            if (isset(self::$_items[$table][$id])) {
135
                self::$_items[$table][$id] = array();
136
            }
137
            self::$_items[$table][$id][$cacheKey] = $data;
138
            DB::query('
139
                INSERT INTO "'.$table.'" ("PAGEID", "CACHEKEY", "DATA")
140
                VALUES ('.$id.', \''.$cacheKey.'\', \''.$data.'\')
141
                ON DUPLICATE KEY UPDATE DATA = \''.$data.'\';
142
            ');
143
        } else {
144
            $cache = SS_Cache::factory($table."_".$id);
145
            $data = serialize($data);
146
            $cache->save($data, $cacheKey);
147
        }
148
    }
149
150
    /**
151
     * @param string           $table
152
     * @param int              $id
153
     * @param string           $cacheKey
154
     * @param mixed            $data
155
     */
156
    public static function touch($table, $id, $cacheKey, $data)
157
    {
158
        $data = self::load($table, $inty, $cacheKey);
159
        self::save($table, $id, $cacheKey, $data);
160
    }
161
162
    private static function make_mysql_table_name($table)
163
    {
164
        return strtoupper($table). '_CACHE';
165
    }
166
}
167

code/api/MysqlCache.php 1 location

@@ 7-165 (lines=159) @@
4
 *
5
 */
6
7
class MysqlCache extends Object implements flushable
8
{
9
    private static $cache_in_mysql_tables = array();
10
11
    public static function flush()
12
    {
13
        $cache = SS_Cache::factory('any');
14
        $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
15
        $tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables');
16
        if (is_array($tables)) {
17
            foreach ($tables as $table) {
18
                $table = self::make_mysql_table_name($table);
19
                DB::query(
20
                    '
21
                    DROP TABLE IF EXISTS "'.$table.'";
22
                    '
23
                );
24
                DB::query(
25
                    '
26
                    CREATE TABLE "'.$table.'" (
27
                      "PAGEID" int(11) NOT NULL,
28
                      "CACHEKEY" CHAR(50) NOT NULL,
29
                      "DATA" TEXT
30
                    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
31
                    '
32
                );
33
34
                DB::query(
35
                    '
36
                    ALTER TABLE "'.$table.'"
37
                      ADD PRIMARY KEY ("PAGEID","CACHEKEY"),
38
                      ADD KEY "CACHEKEY" ("CACHEKEY"),
39
                      ADD KEY "PAGEID" ("PAGEID");
40
                    '
41
                );
42
            }
43
        }
44
    }
45
46
    public static function clean()
47
    {
48
        $tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables');
49
        if (is_array($tables)) {
50
            foreach ($tables as $table) {
51
                $table = self::make_mysql_table_name($table);
52
                DB::query(
53
                    '
54
                    DELETE FROM "'.$table.'";
55
                    '
56
                );
57
            }
58
        }
59
    }
60
61
    private static $_items = array();
62
63
    /**
64
     * @param string           $table
65
     * @param int              $id
66
     * @param string           $cacheKey
67
     * @return mixed
68
     */
69
    public static function load($table, $id, $cacheKey)
70
    {
71
        $tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables');
72
        if (is_array($tables) && in_array($table, $tables)) {
73
            $table = self::make_mysql_table_name($table);
74
            $id = (int)$id;
75
            if (isset(self::$_items[$table])) {
76
                if (isset(self::$_items[$table][$id])) {
77
                    if (isset(self::$_items[$table][$id][$cacheKey])) {
78
                        if (self::$_items[$table][$id][$cacheKey] !== null) {
79
                            return @unserialize(self::$_items[$table][$id][$cacheKey]);
80
                        }
81
                    }
82
                }
83
            }
84
85
            //we are now loading the data ...
86
            if (!isset(self::$_items[$table])) {
87
                self::$_items[$table] = array();
88
            }
89
            if (! isset(self::$_items[$table][$id])) {
90
                self::$_items[$table][$id] = array();
91
                $rows = DB::query('SELECT "CACHEKEY", "DATA" FROM "'.$table.'" WHERE "PAGEID" = '.$id.' ;');
92
                foreach ($rows as $row) {
93
                    self::$_items[$table][$id][$row['CACHEKEY']] = $row['DATA'];
94
                }
95
                //return the value, if there is one.
96
                if (isset(self::$_items[$table][$id])) {
97
                    if (isset(self::$_items[$table][$id][$cacheKey])) {
98
                        return @unserialize(self::$_items[$table][$id][$cacheKey]);
99
                    }
100
                }
101
            }
102
        } else {
103
            $cache = SS_Cache::factory($table.'_'.$id);
104
            $data = $cache->load($cacheKey);
105
            if (!$data) {
106
                return;
107
            }
108
109
            return @unserialize($data);
110
        }
111
    }
112
113
    /**
114
     * @param string           $table
115
     * @param int              $id
116
     * @param string           $cacheKey
117
     * @param mixed            $data
118
     *
119
     */
120
    public static function save($table, $id, $cacheKey, $data)
121
    {
122
        $tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables');
123
        if (is_array($tables) && in_array($table, $tables)) {
124
            $table = self::make_mysql_table_name($table);
125
            $id = (int)$id;
126
            if (strlen($cacheKey) > 50) {
127
                user_error('ERROR: CACHEKEY longer than 50 characters: '.$cacheKey);
128
            }
129
            $data = Convert::raw2sql(serialize($data));
130
            if (!isset(self::$_items[$table])) {
131
                self::$_items[$table] = array();
132
            }
133
            if (isset(self::$_items[$table][$id])) {
134
                self::$_items[$table][$id] = array();
135
            }
136
            self::$_items[$table][$id][$cacheKey] = $data;
137
            DB::query('
138
                INSERT INTO "'.$table.'" ("PAGEID", "CACHEKEY", "DATA")
139
                VALUES ('.$id.', \''.$cacheKey.'\', \''.$data.'\')
140
                ON DUPLICATE KEY UPDATE DATA = \''.$data.'\';
141
            ');
142
        } else {
143
            $cache = SS_Cache::factory($table."_".$id);
144
            $data = serialize($data);
145
            $cache->save($data, $cacheKey);
146
        }
147
    }
148
149
    /**
150
     * @param string           $table
151
     * @param int              $id
152
     * @param string           $cacheKey
153
     * @param mixed            $data
154
     */
155
    public static function touch($table, $id, $cacheKey, $data)
156
    {
157
        $data = self::load($table, $inty, $cacheKey);
158
        self::save($table, $id, $cacheKey, $data);
159
    }
160
161
    private static function make_mysql_table_name($table)
162
    {
163
        return strtoupper($table). '_CACHE';
164
    }
165
}
166