1+ <?php
2+
3+ /**
4+ * JSONDB - JSON Database Manager
5+ *
6+ * Manage local databases with JSON files and JSON Query Language (JQL)
7+ *
8+ * This content is released under the MIT License (MIT)
9+ *
10+ * Copyright (c) 2016, Centers Technologies
11+ *
12+ * Permission is hereby granted, free of charge, to any person obtaining a copy
13+ * of this software and associated documentation files (the "Software"), to deal
14+ * in the Software without restriction, including without limitation the rights
15+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+ * copies of the Software, and to permit persons to whom the Software is
17+ * furnished to do so, subject to the following conditions:
18+ *
19+ * The above copyright notice and this permission notice shall be included in
20+ * all copies or substantial portions of the Software.
21+ *
22+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28+ * THE SOFTWARE.
29+ *
30+ * @package JSONDB
31+ * @author Nana Axel
32+ * @copyright Copyright (c) 2016, Centers Technologies
33+ * @license http://opensource.org/licenses/MIT MIT License
34+ * @filesource
35+ */
36+
37+ namespace JSONDB ;
38+
39+ /**
40+ * Class Cache
41+ * @package JSONDB
42+ * @subpackage Utilities
43+ * @category Cache
44+ * @author Nana Axel
45+ */
46+ class Cache
47+ {
48+ /**
49+ * JSONDB class instance
50+ * @var JSONDB
51+ */
52+ private $ database ;
53+
54+ /**
55+ * Cache array
56+ * @var array
57+ */
58+ private static $ cache = array ();
59+
60+ /**
61+ * Cache __constructor.
62+ *
63+ * @param JSONDB $db JSONDB instance to use with Cache
64+ * @return Cache
65+ */
66+ public function __construct (JSONDB $ db )
67+ {
68+ $ this ->setDatabase ($ db );
69+ }
70+
71+ /**
72+ * Changes the JSONDB instance used
73+ *
74+ * @param JSONDB $database JSONDB class' instance
75+ * @return Cache
76+ */
77+ public function setDatabase ($ database )
78+ {
79+ $ this ->database = $ database ;
80+ $ this ->reset ();
81+ return $ this ;
82+ }
83+
84+ /**
85+ * Gets cached data
86+ * @param array|string $path The path to the table
87+ * @return array|mixed
88+ */
89+ public function get ($ path )
90+ {
91+ if (is_array ($ path )) {
92+ $ results = array ();
93+ foreach ($ path as $ id ) {
94+ $ results [] = $ this ->get ($ id );
95+ }
96+ return $ results ;
97+ }
98+
99+ if (!array_key_exists ($ path , self ::$ cache )) {
100+ self ::$ cache [$ path ] = $ this ->database ->getTableData ($ path );
101+ }
102+
103+ return self ::$ cache [$ path ];
104+ }
105+
106+ /**
107+ * Updates the cached data for a table
108+ * @param string $path The path to the table
109+ * @param array|null $data The data to cache
110+ * @return array
111+ */
112+ public function update ($ path , $ data = NULL )
113+ {
114+ if (NULL !== $ data ) {
115+ self ::$ cache [$ path ] = $ data ;
116+ } else {
117+ self ::$ cache [$ path ] = $ this ->database ->getTableData ($ path );
118+ }
119+ }
120+
121+ /**
122+ * Resets the cache
123+ * @return Cache
124+ */
125+ public function reset ()
126+ {
127+ self ::$ cache = array ();
128+ return $ this ;
129+ }
130+ }
0 commit comments