dbscript
[
class tree: dbscript
] [
index: dbscript
] [
all elements
]
Todo List
Packages:
dbscript
Source for file cache.php
Documentation is available at
cache.php
<?php
/**
* dbscript for PHP 4 & 5 - restful crud framework
*
@version
0.1.2 -- 19-Feb-2007
*
@author
Brian Hendrickson <brian@dbscript.net>
*
@link
http://dbscript.net/
*
@copyright
Copyright 2007 Brian Hendrickson
*
@package
dbscript
*
@license
http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Cache
*
* Incomplete/Experimental
* needs adapting for dbscript
*
* More info...
*
{@link http://dbscript.net/cache}
*
*
@package
dbscript
*
@author
Alejandro Gervasio
*
@access
public
*
@version
0.1.2
*
@todo
implement
*/
class
Cache
{
var
$mysql
;
// instance of MySQL object
var
$result
;
// instance of Result object
var
$expiry
;
// cache expire time in seconds
var
$cacheFile
;
// cache file
var
$data
;
// result set array
// constructor
function
Cache
(
&
$mysql
,
$expiry
=
86400
,
$cacheFile
=
'default_cache.txt'
)
{
$this
->
mysql
=
&
$mysql
;
(
is_int
(
$expiry
)
&&
$expiry
>
0
)
?
$this
->
expiry
=
$expiry
:
$this
->
mysql
->
isError
(
'Expire time must be a positive integer'
)
;
$this
->
cacheFile
=
$cacheFile
;
$this
->
data
=
array
(
)
;
}
// if cache is valid, perform query and return a result set. Otherwise, get results from cache file
function
query
(
$query
)
{
// check if query starts with SELECT
if
(
!
preg_match
(
"/^SELECT/"
,
$query
) )
{
$this
->
mysql
->
isError
(
'Invalid query. Must start with SELECT'
)
;
}
if
(
!
$this
->
isValid
(
) )
{
// read from MySQL
$this
->
result
=
$this
->
mysql
->
query
(
$query
)
;
$this
->
data
=
$this
->
write
(
)
;
}
else
{
// read from cache file
$this
->
data
=
$this
->
read
(
)
;
}
}
// write cache file
function
write
(
)
{
if
(
!
$fp
=
fopen
(
$this
->
cacheFile
,
'w'
) )
{
$this
->
mysql
->
isError
(
'Error opening cache file'
)
;
}
if
(
!
flock
(
$fp
,
LOCK_EX
) )
{
$this
->
mysql
->
isError
(
'Unable to lock cache file'
)
;
}
while
(
$row
=
$this
->
result
->
fetchRow
(
) )
{
$content
[
]
=
$row
;
}
if
(
!
fwrite
(
$fp
,
serialize
(
$content
) ) )
{
$this
->
mysql
->
isError
(
'Error writing to cache file'
)
;
}
flock
(
$fp
,
LOCK_UN
)
;
fclose
(
$fp
)
;
unset
(
$fp
,
$row
)
;
return
$content
;
}
// read cache file
function
read
(
)
{
if
(
!
$content
=
unserialize
(
file_get_contents
(
$this
->
cacheFile
) ) )
{
$this
->
mysql
->
isError
(
'Error reading from cache file'
)
;
}
return
$content
;
}
// determine cache validity based on a time expiry trigger
function
isValid
(
)
{
if
(
file_exists
(
$this
->
cacheFile
)
&&
filemtime
(
$this
->
cacheFile
)
>
(
time
(
)
-
$this
->
expiry
) )
{
return
true
;
}
return
false
;
}
// fetch cache row
function
fetchRow
(
)
{
if
(
!
$row
=
current
(
$this
->
data
) )
{
return
false
;
}
next
(
$this
->
data
)
;
return
$row
;
}
// fetch all cache rows
function
fetchAll
(
)
{
if
(
count
(
$this
->
data
)
<
1
)
{
$this
->
mysql
->
isError
(
'Error accessing cache data'
)
;
}
return
$this
->
data
;
}
// count cache rows
function
countRows
(
)
{
if
(
!
$rows
=
count
(
$this
->
data
) )
{
$this
->
mysql
->
isError
(
'Error counting cache rows'
)
;
}
return
$rows
;
}
}
?>
Documentation generated on Mon, 19 Feb 2007 10:24:41 -0800 by
phpDocumentor 1.3.1