trivia_core /FILECACHE.php

Language PHP Lines 163
MD5 Hash e5318f75a7d38f6dcbc1315ac292c0f3 Estimated Cost $2,967 (why?)
Repository https://bitbucket.org/vodolaz095/trivia_core.git View Raw File View Project SPDX
  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
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
class FileCacheException extends Exception
{

}
class FILECACHE
{
	protected static $instance;
	private $folder='/var/tmp/trivia/';


	private static function init()
	{
		if ( is_null(self::$instance) )
		{
			self::$instance = new FILECACHE();
		}
		return self::$instance;
	}

	private function __construct()
	{
		/* for now - do nothing...*/
	}

    public static function setCacheDir($directory=null)
    {
        $fc=FILECACHE::init();
        if($directory and is_dir($directory))
            {
                if(is_writable($directory))
                    {
                        $fc->folder=$directory;
                    }
                else
                    {
                        throw new FileCacheException('Cache directory '.$directory.' is not writable!');
                    }
            }
        elseif(mkdir($directory,0777))
            {
                $fc->folder=$directory;
            }
        else
            {
                throw new FileCacheException('Cache directory '.$directory.' does not exists and we cannot create it!');
            }
    }

	public static function set($key,$value_or_closure,$duration,$devel=false)
	{
		$ans=FILECACHE::get($key,$devel);
		if($ans)
			{
				return $ans;
			}
		else
			{
				if(is_scalar($value_or_closure))
					$a=$value_or_closure;
				else
					$a=$value_or_closure();

				if($devel)
					$ans='<div title="saved to filecache with key *'.$key.'*" and ttl='.$duration.'>'.$a.'</div>';
				else
					$ans=$a;

				return FILECACHE::setValue($key,$a,$duration,$devel) ? $ans : false;

			}
	}


	protected static function setValue($key,$value,$ttl)
	{
		if(preg_match('~^[a-z0-9_]+$~i',$key))
		{
			$fc=FILECACHE::init();
			$time=$ttl+time();
			foreach (glob($fc->folder.$key.'.*.tmp') as $filename)
			{
				//echo __FUNCTION__.'unlinking '.$filename.PHP_EOL;
				unlink($filename);
			}
			return (file_put_contents($fc->folder.$key.'.'.$time.'.tmp',$value)? $value : false);
		}
		else
		{
			throw new FileCacheException($key.' - is a bad key name!');
		}
	}

	public static  function get($key,$devel=false)
	{
		$fc=FILECACHE::init();
		foreach (glob($fc->folder.$key.'.*.tmp') as $filename)
		{
			$strlen=strlen($fc->folder);
			if(preg_match('~^([a-z0-9_]+)\.(\d+)\.tmp$~i',substr($filename,$strlen),$a))
				{
					$novue=$a[2]-time();
					if($novue>0)
						{
							$a=file_get_contents($filename);
							if($devel) $a='<div title="retrived from filecache with key *'.$key.'* from directory *'.$filename.'*" and ttl='.$novue.'>'.$a.'</div>';
							return $a;
						}
					else
						{
							return false;
						}
				}
			else
				{
					return false;
				}
		}


	}

	public static  function del($key)
	{
		$fc=FILECACHE::init();
		foreach (glob($fc->folder.$key.'.*.tmp') as $filename)
		{
			unlink($filename);
		}
	}

	public static  function flush()
	{
		$fc=FILECACHE::init();
		foreach (glob($fc->folder.'*.tmp') as $filename)
		{
			if(!unlink($filename)) throw new FileCacheException('Unable to remove filecache entry '.$filename.'!');
		}
		return true;
	}


}


/*
//example of code
date_default_timezone_set('UTC');

echo 'Closure test:'.PHP_EOL;
//FILECACHE::flush();
$text='Lalala - setted on '.date('r');
echo date('r').PHP_EOL;
echo FILECACHE::run('lalala',function(){
	return 'closure '.date('r');
},5,true).PHP_EOL;;


//echo 'setting = '.FILECACHE::set('blablabla',date('r'),5,true).PHP_EOL;
echo 'getting = '.FILECACHE::get('blablabla',true).PHP_EOL;
//FILECACHE::flush();
//*/
Back to Top