1<?php
2/*
3 *
4 * Copyright Intermesh
5 *
6 * This file is part of Group-Office. You should have received a copy of the
7 * Group-Office license along with Group-Office. See the file /LICENSE.TXT
8 *
9 * If you have questions write an e-mail to info@intermesh.nl
10 *
11 */
12
13/**
14 * Simple key value store that caches on disk.
15 *
16 * @version $Id: File.class.inc.php 7607 2011-06-15 09:17:42Z mschering $
17 * @copyright Copyright Intermesh BV.
18 * @author Merijn Schering <mschering@intermesh.nl>
19 * @package GO.base.cache
20 */
21
22namespace GO\Base\Cache;
23
24
25class Disk implements CacheInterface{
26
27	private $_ttls;
28	private $_ttlFile;
29	private $_ttlsDirty=false;
30	private $_dir;
31
32	private $_time;
33
34	public function __construct(){
35//		\GO::debug("Using Disk cache");
36
37		$this->_dir = \GO::config()->file_storage_path.'diskcache/';
38
39		if(!is_dir($this->_dir))
40			mkdir($this->_dir, 0777, true);
41
42		$this->_ttlFile = $this->_dir.'ttls.txt';
43		//if(!\GO::config()->debug)
44		$this->_load();
45
46		$this->_time=time();
47	}
48
49	private function _load(){
50		if(!isset($this->_ttls)){
51
52			if(file_exists($this->_ttlFile)){
53				$data = file_get_contents($this->_ttlFile);
54				$this->_ttls = unserialize($data);
55			}else
56			{
57				$this->_ttls = array();
58			}
59		}
60	}
61	/**
62	 * Store any value in the cache
63	 * @param StringHelper $key
64	 * @param mixed $value Will be serialized
65	 * @param int $ttl Seconds to live
66	 */
67	public function set($key, $value, $ttl=0){
68
69		//don't set false values because unserialize returns false on failure.
70		if($value===false)
71			return true;
72
73		$key = \GO\Base\Fs\File::stripInvalidChars($key,'-');
74
75		if($ttl){
76			$this->_ttls[$key]=$this->_time+$ttl;
77			$this->_ttlsDirty=true;
78		}
79
80		return file_put_contents($this->_dir.$key, serialize($value));
81
82	}
83
84	/**
85	 * Get a value from the cache
86	 *
87	 * @param StringHelper $key
88	 * @return boolean
89	 */
90	public function get($key){
91
92		$key = \GO\Base\Fs\File::stripInvalidChars($key, '-');
93
94		if(!empty($this->_ttls[$key]) && $this->_ttls[$key]<$this->_time){
95			unlink($this->_dir.$key);
96			return false;
97		}elseif(!file_exists($this->_dir.$key))
98		{
99			return false;
100		}else
101		{
102			$data = file_get_contents($this->_dir.$key);
103			$unserialized = unserialize($data);
104
105			if($unserialized===false){
106				trigger_error("Could not unserialize key data from file ".$this->_dir.$key);
107				return false;
108			}else
109			{
110				return $unserialized;
111			}
112		}
113	}
114
115	/**
116	 * Delete a value from the cache
117	 *
118	 * @param StringHelper $key
119	 */
120	public function delete($key){
121		$key = \GO\Base\Fs\File::stripInvalidChars($key, '-');
122
123		unset($this->_ttls[$key]);
124		$this->_ttlsDirty=true;
125		if(file_exists($this->_dir.$key)){
126			unlink($this->_dir.$key);
127		}
128	}
129	/**
130	 * Flush all values
131	 */
132	public function flush(){
133		$this->_ttls=array();
134		$this->_ttlsDirty=true;
135		$folder = new \GO\Base\Fs\Folder($this->_dir);
136		$folder->clearContents();
137		//$folder->create(0777);
138	}
139
140	public function __destruct(){
141		if($this->_ttlsDirty)
142			file_put_contents($this->_ttlFile, serialize($this->_ttls));
143	}
144
145	public function supported() {
146		return true;
147	}
148}
149