EcommerceCache::flush()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 16

Duplication

Lines 34
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 0
dl 34
loc 34
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * to do: separate into MEMORY and NON-MEMORY
4
 *
5
 */
6
7 View Code Duplication
class EcommerceCache extends Object implements flushable
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    private static $cache_in_mysql_tables = array(
0 ignored issues
show
Unused Code introduced by
The property $cache_in_mysql_tables is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
157
    {
158
        $data = self::load($table, $inty, $cacheKey);
0 ignored issues
show
Bug introduced by
The variable $inty does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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