summaryrefslogtreecommitdiff
path: root/linkshorter/class/db.php
blob: 77134870d41b2d40358e933122c2828dc1b4a5b3 (plain)
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
<?php

class Database {

	private $db;
	public $error;

	public function __construct($connect = false, $db = false){
		$this->db = new Redis();
		
		if ( ! $connect || ! $db )
			return;

		$this->connect($connect, $db);
	}
	
	public function connect($connect, $db){
		try {
			$this->db->connect($connect);
		} catch (Exception $e){
			$this->error = $e;
			return false;
		}

		try {
			$this->db->select($db);
		} catch (Exception $e){
			$this->error = $e;
			return false;
		}

		try {
			$this->db->ping();
		} catch (Exception $e){
			$this->error = $e;
			return false;
		}
		
		return true;
	}

	public function expire($key, $ttl){
		try {
			$this->db->setTimeout($key, $ttl);
		} catch (Exception $e){
			$this->error = $e;
			return false;
		}
	}

	public function set($key, $value, $ttl = null){
		if ( is_null($ttl) )
			return $this->db->set($key, $value);
		else
			return $this->db->set($key, $value, (int)$ttl);
	}

	public function get($key){
		return $this->db->get($key);
	}

	public function exists($key){
		return $this->db->exists($key);
	}

	public function __destruct(){
		try {
			$this->db->close();
		} catch (Exception $e){
			return false;
		}
	}
}