Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
66e65570
Commit
66e65570
authored
Feb 06, 2007
by
zYne
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
--no commit message
--no commit message
parent
0d6a4e46
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
140 additions
and
44 deletions
+140
-44
Cache.php
lib/Doctrine/Cache.php
+140
-44
No files found.
lib/Doctrine/Cache.php
View file @
66e65570
...
...
@@ -32,35 +32,99 @@
*/
class
Doctrine_Cache
extends
Doctrine_Db_EventListener
implements
Countable
,
IteratorAggregate
{
protected
$_options
=
array
(
'size'
=>
1000
,
'lifeTime'
=>
3600
,
'statsFile'
=>
'tmp/doctrine.cache.stats'
,
/**
* @var array $_options an array of general caching options
*/
protected
$_options
=
array
(
'size'
=>
1000
,
'lifeTime'
=>
3600
,
'statsSlamDefense'
=>
0.75
,
'saveSlamDefense'
=>
0.80
,
'statsFile'
=>
'../data/stats.cache'
,
);
/**
* @var array $_queries query stack
*/
protected
$_queries
=
array
();
/**
* @var Doctrine_Cache_Interface $_driver the cache driver object
*/
protected
$_driver
;
/**
* @var array $data current cache data array
*/
protected
$_data
;
/**
* constructor
*
* @param Doctrine_Cache_Interface|string $driver cache driver name or a driver object
* @param array $options cache driver options
*/
public
function
__construct
(
$driver
,
$options
=
array
())
{
if
(
is_object
(
$driver
))
{
if
(
!
(
$driver
instanceof
Doctrine_Cache_Interface
))
{
throw
new
Doctrine_Cache_Exception
(
'Driver should implement Doctrine_Cache_Interface.'
);
}
$this
->
_driver
=
$driver
;
$this
->
_driver
->
setOptions
(
$options
);
}
else
{
$class
=
'Doctrine_Cache_'
.
ucwords
(
strtolower
(
$driver
));
public
function
__construct
(
$driverName
,
$options
=
array
())
if
(
!
class_exists
(
$class
))
{
throw
new
Doctrine_Cache_Exception
(
'Cache driver '
.
$driver
.
' could not be found.'
);
}
$this
->
_driver
=
new
$class
(
$options
);
}
}
/**
* getDriver
* returns the current cache driver
*
* @return Doctrine_Cache_Driver
*/
public
function
getDriver
()
{
$class
=
'Doctrine_Cache_'
.
ucwords
(
strtolower
(
$driverName
));
return
$this
->
_driver
;
}
/**
* setOption
*
* @param mixed $option the option name
* @param mixed $value option value
* @return boolean TRUE on success, FALSE on failure
*/
public
function
setOption
(
$option
,
$value
)
{
// sanity check (we need this since we are using isset() instead of array_key_exists())
if
(
$value
===
null
)
{
throw
new
Doctrine_Cache_Exception
(
'Null values not accepted for options.'
);
}
if
(
!
class_exists
(
$class
))
{
throw
new
Doctrine_Cache_Exception
(
'Cache driver '
.
$driverName
.
' could not be found.'
);
if
(
isset
(
$this
->
_options
[
$option
]))
{
$this
->
_options
[
$option
]
=
$value
;
return
true
;
}
$this
->
_driver
=
new
$class
(
$options
);
return
false
;
}
public
function
getDriver
()
/**
* getOption
*
* @param mixed $option the option name
* @return mixed option value
*/
public
function
getOption
(
$option
)
{
return
$this
->
_driver
;
if
(
!
isset
(
$this
->
_options
[
$option
]))
{
throw
new
Doctrine_Cache_Exception
(
'Unknown option '
.
$option
);
}
return
$this
->
_options
[
$option
];
}
/**
* addQuery
* add
* adds a query to internal query stack
*
* @param string|array $query sql query string
* @param string $namespace connection namespace
...
...
@@ -172,10 +236,24 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
*/
public
function
flush
()
{
file_put_contents
(
$this
->
_statsFile
,
implode
(
"
\n
"
,
$this
->
queries
));
}
if
(
$this
->
_options
[
'statsFile'
]
!==
false
)
{
if
(
!
file_exists
(
$this
->
_options
[
'statsFile'
]))
{
throw
new
Doctrine_Cache_Exception
(
"Couldn't save cache statistics. Cache statistics file doesn't exists!"
);
}
file_put_contents
(
$this
->
_options
[
'statsFile'
],
implode
(
"
\n
"
,
$this
->
_queries
));
}
}
/**
* onPreQuery
* listens the onPreQuery event of Doctrine_Db
*
* adds the issued query to internal query stack
* and checks if cached element exists
*
* @return boolean
*/
public
function
onPreQuery
(
Doctrine_Db_Event
$event
)
{
$query
=
$event
->
getQuery
();
...
...
@@ -194,36 +272,44 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
return
false
;
}
public
function
onQuery
(
Doctrine_Db_Event
$event
)
/**
* onPreFetch
* listens the onPreFetch event of Doctrine_Db_Statement
*
* advances the internal pointer of cached data and returns
* the current element
*
* @return array
*/
public
function
onPreFetch
(
Doctrine_Db_Event
$event
)
{
$ret
=
current
(
$this
->
_data
);
next
(
$this
->
_data
);
return
$ret
;
}
/**
* onPreFetch
* listens the onPreFetchAll event of Doctrine_Db_Statement
*
* returns the current cache data array
*
* @return array
*/
public
function
onPreFetchAll
(
Doctrine_Db_Event
$event
)
{
return
$this
->
_data
;
}
public
function
onPrePrepare
(
Doctrine_Db_Event
$event
)
{
}
public
function
onPrepare
(
Doctrine_Db_Event
$event
)
{
}
public
function
onPreExec
(
Doctrine_Db_Event
$event
)
{
}
public
function
onExec
(
Doctrine_Db_Event
$event
)
{
}
/**
* onPreExecute
* listens the onPreExecute event of Doctrine_Db_Statement
*
* adds the issued query to internal query stack
* and checks if cached element exists
*
* @return boolean
*/
public
function
onPreExecute
(
Doctrine_Db_Event
$event
)
{
{
$query
=
$event
->
getQuery
();
// only process SELECT statements
...
...
@@ -240,7 +326,17 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
return
false
;
}
public
function
onExecute
(
Doctrine_Db_Event
$event
)
/**
* destructor
*
* @return void
*/
public
function
process
()
{
$rand
=
(
rand
(
1
,
10000
)
/
(
10000
*
100
));
if
(
$rand
>
$this
->
_options
[
'statSlamDefense'
])
{
$this
->
flush
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment