Completed
Push — release-2.1 ( 6812c1...f33e38 )
by John
06:54
created

sqlite_cache::connect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Simple Machines Forum (SMF)
5
 *
6
 * @package SMF
7
 * @author Simple Machines http://www.simplemachines.org
8
 * @copyright 2017 Simple Machines and individual contributors
9
 * @license http://www.simplemachines.org/about/smf/license.php BSD
10
 *
11
 * @version 2.1 Beta 3
12
 */
13
14
if (!defined('SMF'))
15
	die('Hacking attempt...');
16
17
/**
18
 * SQLite Cache API class
19
 * @package cacheAPI
20
 */
21
class sqlite_cache extends cache_api
22
{
23
	/**
24
	 * @var string The path to the current $cachedir directory.
25
	 */
26
	private $cachedir = null;
27
	private $cacheDB = null;
28
	private $cacheTime = null;
29
30
	public function __construct()
31
	{
32
33
		parent::__construct();
34
35
		// Set our default cachedir.
36
		$this->setCachedir();
37
38
	}
39
40
	/**
41
	 * {@inheritDoc}
42
	 */
43
	public function connect()
44
	{
45
46
		$database = $this->cachedir . '/' . 'SQLite3Cache.db3';
47
		$this->cacheDB = new SQLite3($database);
48
		$this->cacheDB->busyTimeout(1000);
49
		if (filesize($database) == 0)
50
		{
51
			$this->cacheDB->exec('CREATE TABLE cache (key text unique, value blob, ttl int);');
52
			$this->cacheDB->exec('CREATE INDEX ttls ON cache(ttl);');
53
		}
54
		$this->cacheTime = time();
55
56
	}
57
58
	/**
59
	 * {@inheritDoc}
60
	 */
61 View Code Duplication
	public function isSupported($test = false)
0 ignored issues
show
Duplication introduced by
This method 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...
62
	{
63
		$supported = class_exists("SQLite3") && is_writable($this->cachedir);
64
65
		if ($test)
66
		{
67
			return $supported;
68
		}
69
70
		return parent::isSupported() && $supported;
71
	}
72
73
	/**
74
	 * {@inheritDoc}
75
	 */
76
	public function getData($key, $ttl = null)
77
	{
78
		$ttl = time() - $ttl;
79
		$query = 'SELECT value FROM cache WHERE key = \'' . $this->cacheDB->escapeString($key) . '\' AND ttl >= ' . $ttl . ' LIMIT 1';
80
		$result = $this->cacheDB->query($query);
81
82
		$value = null;
83
		while ($res = $result->fetchArray(SQLITE3_ASSOC))
84
		{
85
			$value = $res['value'];
86
		}
87
88
		return !empty($value) ? $value : null;
89
	}
90
91
	/**
92
	 * {@inheritDoc}
93
	 */
94
	public function putData($key, $value, $ttl = null)
95
	{
96
97
		$ttl = $this->cacheTime + $ttl;
98
		$query = 'REPLACE INTO cache VALUES (\'' . $this->cacheDB->escapeString($key) . '\', \'' . $this->cacheDB->escapeString($value) . '\', ' . $this->cacheDB->escapeString($ttl) . ');';
99
		$result = $this->cacheDB->exec($query);
100
101
		return $result;
102
	}
103
104
	/**
105
	 * {@inheritDoc}
106
	 */
107
	public function cleanCache($type = '')
108
	{
109
110
		$query = 'DELETE FROM cache;';
111
		$result = $this->cacheDB->exec($query);
112
113
		return $result;
114
115
	}
116
117
	/**
118
	 * {@inheritDoc}
119
	 */
120 View Code Duplication
	public function cacheSettings(array &$config_vars)
0 ignored issues
show
Duplication introduced by
This method 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...
121
	{
122
		global $context, $txt;
123
124
		$config_vars[] = $txt['cache_sqlite_settings'];
125
		$config_vars[] = array('cachedir_sqlite', $txt['cachedir_sqlite'], 'file', 'text', 36, 'cache_sqlite_cachedir');
126
127
		if (!isset($context['settings_post_javascript']))
128
		{
129
			$context['settings_post_javascript'] = '';
130
		}
131
132
		$context['settings_post_javascript'] .= '
133
			$("#cache_accelerator").change(function (e) {
134
				var cache_type = e.currentTarget.value;
135
				$("#cachedir_sqlite").prop("disabled", cache_type != "sqlite");
136
			});';
137
	}
138
139
	/**
140
	 * Sets the $cachedir or uses the SMF default $cachedir..
141
	 *
142
	 * @access public
143
	 *
144
	 * @param string $dir A valid path
145
	 *
146
	 * @return boolean If this was successful or not.
147
	 */
148 View Code Duplication
	public function setCachedir($dir = null)
0 ignored issues
show
Duplication introduced by
This method 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...
149
	{
150
		global $cachedir_sqlite;
151
152
		// If its invalid, use SMF's.
153
		if (is_null($dir) || !is_writable($dir))
154
		{
155
			$this->cachedir = $cachedir_sqlite;
156
		}
157
		else
158
		{
159
			$this->cachedir = $dir;
160
		}
161
	}
162
163
}
164