summaryrefslogtreecommitdiff
path: root/linkshorter/class
diff options
context:
space:
mode:
authorroot2014-11-27 02:41:21 +0100
committerroot2014-11-27 02:41:21 +0100
commit92babb43a2a0041a71b54db35cbc9d2fba908a63 (patch)
tree8971101b53b6a6b12a092b456dc11a4dbfc034a3 /linkshorter/class
parent2b24f3ef0e800f878177973eefcb28380a292503 (diff)
downloadtools.iamfabulous.de-92babb43a2a0041a71b54db35cbc9d2fba908a63.tar.gz
Rewrote the linkshorter.
Diffstat (limited to 'linkshorter/class')
-rw-r--r--linkshorter/class/db.php73
1 files changed, 73 insertions, 0 deletions
diff --git a/linkshorter/class/db.php b/linkshorter/class/db.php
new file mode 100644
index 0000000..7713487
--- /dev/null
+++ b/linkshorter/class/db.php
@@ -0,0 +1,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;
+ }
+ }
+}