├── .gitignore ├── README.md └── RedisLite.php /.gitignore: -------------------------------------------------------------------------------- 1 | # OS generated files # 2 | ###################### 3 | ._* 4 | .Apple* 5 | .DS_Store 6 | ehthumbs.db 7 | Icon? 8 | Thumbs.db 9 | /nbproject 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | RedisLite 2 | ========= 3 | 4 | A simple storage class inspired by the redis api on top of Sqlite. 5 | 6 | # Usage 7 | 8 | $db = new RedisLite("PATH_TO_WRITABLE_FOLDER/mydb"); 9 | 10 | $db->set("mykey", 1); 11 | 12 | $db->incr("mykey"); 13 | 14 | $db->get("mykey"); // 2 15 | 16 | // Lists 17 | 18 | db->rpush("mylist", "item1"); 19 | db->rpush("mylist", "item2"); 20 | $db->lpush("mylist", "item3"); 21 | 22 | $db->get("mylist"); // ["item3", "item1", "item2"] 23 | 24 | // hashes 25 | 26 | $db->hset("myhash", "myfield", 1); 27 | $db->hmset("myhash", "myfield-1", "value-1", "myfield-2", "value-2"); 28 | 29 | $db->hkeys("myhash"); // ["myfield", "myfield-1", "myfield-2"] 30 | $db->hvals("myhash"); // [1, "value-1", "value-2"] 31 | 32 | $db->hget("myhash", "myfield"); // 1 33 | 34 | $db->get("myhash"); // array("myfield"=>1, "myfield-1"=>"value-1", "myfield-2"=>"value-2") 35 | 36 | ## implemented methods 37 | 38 | set, get, exists, keys, del, type, incr, decr, 39 | llen, lpush, rpush, lset, lindex, 40 | hset, hget, hgetall, hexists, hkeys, hvals, hlen, hincrby, hmset, hmget 41 | -------------------------------------------------------------------------------- /RedisLite.php: -------------------------------------------------------------------------------- 1 | "storage"), $options); 32 | $dns = "sqlite:{$path}"; 33 | 34 | $this->path = $path; 35 | $this->table = $options["storagetable"]; 36 | $this->connection = new \PDO($dns, null, null, $options); 37 | 38 | $stmt = $this->connection->query("SELECT name FROM sqlite_master WHERE type='table' AND name='".$this->table."';"); 39 | $table = $stmt->fetch(\PDO::FETCH_ASSOC); 40 | 41 | if (!isset($table["name"])) { 42 | $this->createTable(); 43 | } 44 | } 45 | 46 | protected function createTable() { 47 | $this->connection->exec("CREATE TABLE ".$this->table." (key VARCHAR PRIMARY KEY, keyval TEXT)"); 48 | $this->connection->exec("CREATE UNIQUE INDEX key_name on ".$this->table." (key);"); 49 | } 50 | 51 | /** 52 | * Get value for specific key 53 | * 54 | * @param string $key 55 | * @param mixed $default 56 | * @return mixed 57 | */ 58 | public function get($key, $default = null) { 59 | 60 | $stmt = $this->connection->query("SELECT * FROM ".$this->table." WHERE `key`='{$key}';"); 61 | $res = $stmt->fetch(\PDO::FETCH_ASSOC); 62 | 63 | return isset($res["key"]) ? json_decode($res["keyval"], true) : $default; 64 | } 65 | 66 | /** 67 | * Set value for specific key 68 | * 69 | * @param string $key 70 | * @param mixed $value 71 | */ 72 | public function set($key, $value) { 73 | 74 | $value = $this->connection->quote(json_encode($value, JSON_NUMERIC_CHECK)); 75 | 76 | if($this->exists($key)) { 77 | $sql = "UPDATE ".$this->table." SET `keyval`={$value} WHERE `key`='{$key}'"; 78 | } else { 79 | $sql = "INSERT INTO ".$this->table." (`key`,`keyval`) VALUES ('{$key}',{$value})"; 80 | } 81 | 82 | $this->connection->exec($sql); 83 | } 84 | 85 | /** 86 | * Clear database 87 | * 88 | */ 89 | public function flushdb() { 90 | $this->connection->exec("DELETE FROM ".$this->table); 91 | } 92 | 93 | /** 94 | * Check if key exists 95 | * 96 | * @param string $key 97 | */ 98 | public function exists($key) { 99 | 100 | $stmt = $this->connection->query("SELECT `key` FROM ".$this->table." WHERE `key`='{$key}';"); 101 | $res = $stmt->fetch(\PDO::FETCH_ASSOC); 102 | 103 | return isset($res["key"]); 104 | } 105 | 106 | /** 107 | * Get all keys matching a pattern 108 | * 109 | * @param string $pattern 110 | * @return array 111 | */ 112 | public function keys($pattern = null) { 113 | 114 | $keys = array(); 115 | $stmt = $this->connection->query("SELECT `key` FROM ".$this->table." ORDER BY `key`;"); 116 | $res = $stmt->fetchAll(\PDO::FETCH_ASSOC); 117 | 118 | if (!$pattern) { 119 | 120 | foreach ($res as $record) { 121 | $keys[] = $record["key"]; 122 | } 123 | 124 | } else { 125 | 126 | $matcher = function_exists('fnmatch') ? 'fnmatch': function($pattern, $string){ 127 | return preg_match("#^".strtr(preg_quote($pattern, '#'), array('\*' => '.*', '\?' => '.'))."$#i", $string); 128 | }; 129 | 130 | foreach ($res as $record) { 131 | if($matcher($pattern, $record["key"])) { 132 | $keys[] = $record["key"]; 133 | } 134 | } 135 | } 136 | 137 | return $keys; 138 | } 139 | 140 | /** 141 | * Delete Key(s) 142 | * 143 | * @param string $key 144 | * @return integer 145 | */ 146 | public function del($key) { 147 | 148 | $keys = func_get_args(); 149 | $removed = 0; 150 | 151 | foreach ($keys as $key) { 152 | $sql = 'DELETE FROM '.$this->table.' WHERE `key`="'.$key.'"'; 153 | $this->connection->exec($sql); 154 | $removed++; 155 | } 156 | 157 | return $removed; 158 | } 159 | 160 | /** 161 | * Get value type 162 | * 163 | * @param string $key 164 | * @return string 165 | */ 166 | public function type($key) { 167 | 168 | $value = $this->get($key, null); 169 | 170 | return gettype($value); 171 | } 172 | 173 | /** 174 | * Increment value by x 175 | * 176 | * @param string $key 177 | * @param integer $by 178 | * @return integer 179 | */ 180 | public function incr($key, $by = 1) { 181 | 182 | $current = $this->get($key, 0); 183 | $newone = $current + $by; 184 | 185 | $this->set($key, $newone); 186 | 187 | return $newone; 188 | } 189 | 190 | /** 191 | * Decrement value by x 192 | * 193 | * @param string $key 194 | * @param integer $by 195 | * @return integer 196 | */ 197 | public function decr($key, $by = 1) { 198 | 199 | return $this->incr($key, ($by * -1)); 200 | } 201 | 202 | /** 203 | * Count $value items 204 | * 205 | * @param string $key 206 | * @return integer 207 | */ 208 | public function llen($key) { 209 | 210 | $value = $this->get($key, array()); 211 | 212 | return is_array($value) ? count($value):0; 213 | } 214 | 215 | /** 216 | * Add item to a value (right) 217 | * 218 | * @param string $key 219 | * @param mixed $value 220 | * @return integer 221 | */ 222 | public function rpush($key, $value) { 223 | 224 | $list = $this->get($key, array()); 225 | 226 | $list[] = $value; 227 | 228 | $this->set($key, $list); 229 | 230 | return count($list); 231 | } 232 | 233 | /** 234 | * Add item to a value (left) 235 | * 236 | * @param string $key 237 | * @param mixed $value 238 | * @return integer 239 | */ 240 | public function lpush($key, $value) { 241 | 242 | $list = $this->get($key, array()); 243 | 244 | array_unshift($list, $value); 245 | 246 | $this->set($key, $list); 247 | 248 | return count($list); 249 | } 250 | 251 | /** 252 | * Set the value of an element in a list by its index 253 | * 254 | * @param string $key 255 | * @param integer $index 256 | * @param mixed $value 257 | * @return boolean 258 | */ 259 | public function lset($key, $index, $value) { 260 | 261 | $list = $this->get($key, array()); 262 | 263 | if($index < 0) { 264 | $index = count($list) - abs($index); 265 | } 266 | 267 | if(isset($list[$index])){ 268 | $list[$index] = $value; 269 | $this->set($key, $list); 270 | 271 | return true; 272 | } 273 | 274 | return false; 275 | } 276 | 277 | /** 278 | * Get an element from a list by its index 279 | * 280 | * @param string $key 281 | * @param integer $index 282 | * @return mixed 283 | */ 284 | public function lindex($key, $index) { 285 | 286 | $list = $this->get($key, array()); 287 | 288 | if($index < 0) { 289 | $index = count($list) - abs($index); 290 | } 291 | 292 | return isset($list[$index]) ? $list[$index]:null; 293 | } 294 | 295 | /** 296 | * Set the string value of a hash field 297 | * 298 | * @param string $key 299 | * @param string $field 300 | * @param mixed $value 301 | */ 302 | public function hset($key, $field, $value) { 303 | 304 | $set = $this->get($key, array()); 305 | 306 | $set[$field] = $value; 307 | $this->set($key, $set); 308 | } 309 | 310 | /** 311 | * Get the value of a hash field 312 | * 313 | * @param string $key 314 | * @param string $field 315 | * @param mixed $default 316 | * @return mixed 317 | */ 318 | public function hget($key, $field, $default=null) { 319 | 320 | $set = $this->get($key, array()); 321 | 322 | return isset($set[$field]) ? $set[$field]:$default; 323 | } 324 | 325 | /** 326 | * Get all the fields and values in a hash 327 | * 328 | * @param string $key 329 | * @return array 330 | */ 331 | public function hgetall($key) { 332 | 333 | $set = $this->get($key, array()); 334 | 335 | return $set; 336 | } 337 | 338 | /** 339 | * Determine if a hash field exists 340 | * 341 | * @param string $key 342 | * @param string $field 343 | * @return boolean 344 | */ 345 | public function hexists($key, $field) { 346 | 347 | $set = $this->get($key, array()); 348 | 349 | return isset($set[$field]); 350 | } 351 | 352 | /** 353 | * Get all the fields in a hash 354 | * 355 | * @param string $key 356 | * @return array 357 | */ 358 | public function hkeys($key) { 359 | 360 | $set = $this->get($key, array()); 361 | 362 | return array_keys($set); 363 | } 364 | 365 | /** 366 | * Get all the values in a hash 367 | * 368 | * @param string $key 369 | * @return array 370 | */ 371 | public function hvals($key) { 372 | 373 | $set = $this->get($key, array()); 374 | 375 | return array_values($set); 376 | } 377 | 378 | /** 379 | * Get the number of fields in a hash 380 | * 381 | * @param string $key 382 | * @return integer 383 | */ 384 | public function hlen($key) { 385 | 386 | return count($this->hkeys($key)); 387 | } 388 | 389 | /** 390 | * Delete one or more hash fields 391 | * 392 | * @param string $key 393 | * @return integer 394 | */ 395 | public function hdel($key) { 396 | 397 | $set = $this->get($key, array()); 398 | 399 | if(!count($set)) return 0; 400 | 401 | $fields = func_get_args(); 402 | $removed = 0; 403 | 404 | for ($i=1; $iset($key, $set); 415 | 416 | return $removed; 417 | } 418 | 419 | /** 420 | * Increment the integer value of a hash field by the given number 421 | * 422 | * @param string $key 423 | * @param string $field 424 | * @param integer $by 425 | * @return integer 426 | */ 427 | public function hincrby($key, $field, $by = 1) { 428 | 429 | $current = $this->hget($key, $field, 0); 430 | $newone = $current+$by; 431 | 432 | $this->hset($key, $field, $newone); 433 | 434 | return $newone; 435 | } 436 | 437 | /** 438 | * Get the values of all the given hash fields 439 | * 440 | * @param string $key 441 | * @return array 442 | */ 443 | public function hmget($key) { 444 | 445 | $set = $this->get($key, array()); 446 | $fields = func_get_args(); 447 | $values = array(); 448 | 449 | for ($i=1; $iget($key, array()); 465 | $args = func_get_args(); 466 | 467 | for ($i=1; $iset($key, $set); 476 | } 477 | } 478 | --------------------------------------------------------------------------------