SqliteCache   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 121
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A get() 0 15 1
A save() 0 28 4
A clean() 0 8 2
A remove() 0 7 1
1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   Kumbia
11
 * @package    Cache
12
 * @subpackage Drivers
13
 *
14
 * @copyright  Copyright (c) 2005 - 2019 KumbiaPHP Team (http://www.kumbiaphp.com)
15
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
16
 */
17
18
/**
19
 * Cache con Sqlite
20
 *
21
 * @category   Kumbia
22
 * @package    Cache
23
 * @subpackage Drivers
24
 */
25
class SqliteCache extends Cache
26
{
27
28
    /**
29
     * Conexion a la base de datos Sqlite
30
     *
31
     * @var resource
32
     * */
33
    protected $_db = null;
34
35
    /**
36
     * Constructor
37
     *
38
     * */
39
    public function __construct()
40
    {
41
        /**
42
         * Abre una conexión SqLite a la base de datos cache
43
         *
44
         */
45
        $this->_db = sqlite_open(APP_PATH . 'temp/cache.db');
46
        $result = sqlite_query($this->_db, "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND tbl_name='cache' ");
47
        $count = sqlite_fetch_single($result);
48
49
        if (!$count) {
50
            sqlite_exec(' CREATE TABLE cache (id TEXT, "group" TEXT, value TEXT, lifetime TEXT) ', $this->_db);
51
        }
52
    }
53
54
    /**
55
     * Carga un elemento cacheado
56
     *
57
     * @param string $id
58
     * @param string $group
59
     * @return string
60
     */
61
    public function get($id, $group='default')
62
    {
63
        $this->_id = $id;
64
        $this->_group = $group;
65
66
        $id = addslashes($id);
67
        $group = addslashes($group);
68
69
        $id = addslashes($id);
70
        $group = addslashes($group);
71
        $lifetime = time();
72
73
        $result = sqlite_query($this->_db, " SELECT value FROM cache WHERE id='$id' AND \"group\"='$group' AND lifetime>'$lifetime' OR lifetime='undefined' ");
74
        return sqlite_fetch_single($result);
75
    }
76
77
    /**
78
     * Guarda un elemento en la cache con nombre $id y valor $value
79
     *
80
     * @param string $id
81
     * @param string $group
82
     * @param string $value
83
     * @param int $lifetime tiempo de vida en forma timestamp de unix
84
     * @return boolean
85
     */
86
    public function save($value, $lifetime='', $id='', $group='default')
87
    {
88
        if (!$id) {
89
            $id = $this->_id;
90
            $group = $this->_group;
91
        }
92
93
        if ($lifetime) {
94
            $lifetime = strtotime($lifetime);
95
        } else {
96
            $lifetime = 'undefined';
97
        }
98
99
        $id = addslashes($id);
100
        $group = addslashes($group);
101
        $value = addslashes($value);
102
103
        $result = sqlite_query($this->_db, " SELECT COUNT(*) FROM cache WHERE id='$id' AND \"group\"='$group' ");
104
        $count = sqlite_fetch_single($result);
105
106
107
        // Ya existe el elemento cacheado
108
        if ($count) {
109
            return sqlite_exec(" UPDATE cache SET value='$value', lifetime='$lifetime' WHERE id='$id' AND \"group\"='$group' ", $this->_db);
110
        }
111
112
        return sqlite_exec(" INSERT INTO cache (id, \"group\", value, lifetime) VALUES ('$id','$group','$value','$lifetime')", $this->_db);
113
    }
114
115
    /**
116
     * Limpia la cache
117
     *
118
     * @param string $group
119
     * @return boolean
120
     */
121
    public function clean($group='')
122
    {
123
        if ($group) {
124
            $group = addslashes($group);
125
            return sqlite_exec(" DELETE FROM cache WHERE \"group\"='$group' ", $this->_db);
126
        }
127
        return sqlite_exec(" DELETE FROM cache ", $this->_db);
128
    }
129
130
    /**
131
     * Elimina un elemento de la cache
132
     *
133
     * @param string $id
134
     * @param string $group
135
     * @return boolean
136
     */
137
    public function remove($id, $group='default')
138
    {
139
        $id = addslashes($id);
140
        $group = addslashes($group);
141
142
        return sqlite_exec(" DELETE FROM cache WHERE id='$id' AND \"group\"='$group' ", $this->_db);
143
    }
144
145
}
146