summaryrefslogtreecommitdiff
path: root/class/redis.php
blob: 65c9312fc69e89d67428d6c7b6fc7f98405d6e86 (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
<?php
class database {
	
	public $db;
	private $SOCKET;
	private $DBNAME;

	public function __construct($DBNAME, $SOCKET) {
		$this->db = new Redis();
		$this->DBNAME = $DBNAME;
		$this->SOCKET = $SOCKET;
	}

	public function open() {
		try {
			$this->db->connect($this->SOCKET);
		}
		catch (Exception $e) {
			failure($e->getMessage());
		}
		if(!$this->db->ping())
			failure("No connection to database established.");

		if(!$this->db->select($this->DBNAME))
			failure("No connection to database established.");

		return true;
	}

	public function close() {
		$this->db->close();
	}

	public function storeList($KEY, $VALUE) {
		for ($i=0; $i<count($VALUE); $i++){
			$this->db->rPush($KEY, $VALUE[$i]);
		}
	}

	public function getAll($KEY){
		$len = $this->db->lSize($KEY);
		$list = array();
		for($i=0;$i<$len;$i++) {
			$list[$i] = $this->db->lGet($KEY, $i);
		}
		
		return $list;
	}

	public function getItem($KEY, $INDEX){
		return $this->db->lget($KEY, $INDEX);
	}

	public function listExists($KEY) {
		if($this->db->lLen($KEY) == 0){
			return false;
		}
		return true;
	}

	public function len($KEY){
		return $this->db->lLen($KEY);
	}
}