public
Description: Midgard Components Framework 3rd generation
Homepage: http://www.midgard-project.org
Clone URL: git://github.com/bergie/midcom.git
midcom / midcom_core / services / cache / sqlite.php
100644 169 lines (153 sloc) 5.603 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* @package midcom_core
* @author The Midgard Project, http://www.midgard-project.org
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
 
/**
* SQLite cache backend.
*
* @package midcom_core
*/
class midcom_core_services_cache_sqlite implements midcom_core_services_cache
{
    private $_db;
    private $_table;
    
    public function __construct()
    {
        $this->_db = new SQLiteDatabase("{$_MIDCOM->configuration->cache_directory}/{$_MIDCOM->configuration->cache_name}.sqlite");
        $this->_table = str_replace(array(
            '.', '-'
        ), '_', $this->_name);
        
        // Check if we have a DB table corresponding to current cache name
        $result = $this->_db->query("SELECT name FROM sqlite_master WHERE type='table' AND name='{$this->_table}'");
        $tables = $result->fetchAll();
        if (count($tables) == 0)
        {
            /**
* Creating table for data
*/
            $this->_db->query("CREATE TABLE {$this->_table} (key VARCHAR(255), value TEXT);");
            $this->_db->query("CREATE INDEX {$this->_table}_key ON {$this->_table} (key);");
            
            /**
* Creating table for tags
*/
            $this->_db->query("CREATE TABLE {$this->_table}_tags (key VARCHAR(255), tag VARCHAR(255));");
            $this->_db->query("CREATE INDEX {$this->_table}_tags ON {$this->_table}_tags (key, tag);");
        }
    }
    
    public function get($key)
    {
        $key = sqlite_escape_string($key);
        $results = $this->_db->query("SELECT value FROM {$this->_table} WHERE key='{$key}'");
        $results = $results->fetchAll();
        if (count($results) == 0)
        {
            return false; // no hit
        }
        
        return $results[0]['value'];
    }
    
    public function get_by_tag($tags)
    {
        $constraint = '';
        if (is_array($tags))
        {
            foreach ($tags as $tag)
            {
                $tag = sqlite_escape_string($tag);
                $constraint .= "{$this->_table}_tags.tag='{$tag}' OR ";
            }
            $constraint = substr($constraint, 0, strlen($constraint) - 3);
        }
        else
        {
            $tags = sqlite_escape_string($tags);
            $constraint = "{$this->_table}_tags.tag='{$tag}'";
        }
        // Making a query
        $query = ("SELECT {$this->_table}.key AS key, {$this->_table}.value AS value FROM {$this->_table}
LEFT JOIN {$this->_table}_tags ON {$this->_table}_tags.key={$this->_table}.key
WHERE $constraint
");
        
        $results = $this->_db->query($query);
        $results = $results->fetchAll();
        if (count($results) == 0)
        {
            return false; // no hit
        }
        
        return $results;
    }
    
    public function put($key, $data, $timeout = false, $tags = null)
    {
        $key = sqlite_escape_string($key);
        $data = sqlite_escape_string($data);
        $this->_db->query("REPLACE INTO {$this->_table} (key, value) VALUES ('{$key}', '{$data}')");
        if (! is_null($tags))
        {
            if (is_array($tags))
            {
                foreach ($tags as $tag)
                {
                    $tag = sqlite_escape_string($tag);
                    $tag_id = $this->checktag($tag);
                    $this->_db->query("REPLACE INTO {$this->_table}_tags (tag, key) VALUES ('{$tag}', '{$key}')");
                }
            }
            else
            {
                $tags = sqlite_escape_string($tags);
                $tag_id = $this->checktag($tags);
                $this->_db->query("REPLACE INTO {$this->_table}_tags (tag, key) VALUES ('{$tag}', '{$key}')");
            }
        }
    }
    
    public function remove($key)
    {
        $key = sqlite_escape_string($key);
        $this->_db->query("DELETE FROM {$this->_table} WHERE key='{$key}'");
        $this->_db->query("DELETE FROM {$this->_table}_tags WHERE key='{$key}'");
    }
    
    public function remove_by_tags($tags)
    {
        if (is_array($tags))
        {
            foreach ($tags as $tag)
            {
                $tag = sqlite_escape_string($tag);
                $results = $this->_db->query("SELECT key FROM {$this->_table}_tags WHERE tag='{$tag}'");
                $results = $results->fetchAll();
                foreach ($results as $r)
                {
                    $this->_db->query("DELETE FROM {$this->_table} WHERE key='{$r['key']}");
                    $this->_db->query("DELETE FROM {$this->_table}_tags WHERE key='{$r['key']}'");
                }
            }
        }
        else
        {
            $tags = sqlite_escape_string($tags);
            $results = $this->_db->query("SELECT key FROM {$this->_table}_tags WHERE tag='{$tags}'");
            $results = $results->fetchAll();
            foreach ($results as $r)
            {
                $this->_db->query("DELETE FROM {$this->_table} WHERE key='{$r['key']}");
                $this->_db->query("DELETE FROM {$this->_table}_tags WHERE key='{$r['key']}'");
            }
        }
    }
 
    public function remove_all()
    {
        $this->_db->query("DELETE FROM {$this->_table} WHERE 1");
        $this->_db->query("DELETE FROM {$this->_table}_tags WHERE 1");
    }
    
    public function exists($key)
    {
        if($this->get($key) == false)
        {
            return false;
        }
        return true;
    }
    
    
}
?>