Skip to content
Browse files

Fixed count cache

Pity, but in memory sqlite is shit - it works per worker and may used different storages for multiple php workers. Move back to files...
  • Loading branch information...
1 parent 6a591fe commit c19409cc3834dbb189fd9789c67eeb942ab0897f @smgladkovskiy committed Oct 27, 2011
Showing with 28 additions and 19 deletions.
  1. +26 −17 classes/cache/sqlite.php
  2. +2 −2 classes/caching/jelly/builder.php
View
43 classes/cache/sqlite.php
@@ -21,15 +21,13 @@ protected function __construct(array $config)
}
// Load new Sqlite DB
- $this->_db = new PDO('sqlite:'.$database,'', '', array(
- PDO::ATTR_PERSISTENT => TRUE
- ));
+ $this->_db = new PDO('sqlite:'.$database,'', '', $this->_config['PDO_config']);
// Test for existing DB
- $result = $this->_db->query("SELECT * FROM sqlite_master WHERE name = 'caches".$this->_config['suffix']."' AND type = 'table'")->fetchAll();
+ $result = $this->_db->query("SELECT * FROM sqlite_master WHERE name = 'caches' AND type = 'table'")->fetchAll();
// If there is no table, create a new one
- if (0 == count($result))
+ if (count($result) == 0)
{
$database_schema = Arr::get($this->_config, 'schema', NULL);
@@ -41,11 +39,22 @@ protected function __construct(array $config)
try
{
// Create the caches table
- $this->_db->query(Arr::get($this->_config, 'schema', NULL));
+ $this->_db->query($database_schema);
}
catch (PDOException $e)
{
- throw new Cache_Exception('Failed to create new SQLite caches'.$this->_config['suffix'].' table with the following error : :error', array(':error' => $e->getMessage()));
+ throw new Cache_Exception('Failed to create new SQLite caches table with the following error : :error', array(':error' => $e->getMessage()));
+ }
+ }
+ else
+ {
+ $gc = $this->_config['gc_probability'];
+
+ // If the GC probability is a hit
+ if (rand(0,99) <= $gc)
+ {
+ // Garbage Collect
+ $this->garbage_collect();
}
}
}
@@ -61,7 +70,7 @@ protected function __construct(array $config)
public function get($id, $default = NULL)
{
// Prepare statement
- $statement = $this->_db->prepare('SELECT id, expiration, cache FROM caches'.$this->_config['suffix'].' WHERE id = :id LIMIT 0, 1');
+ $statement = $this->_db->prepare('SELECT id, expiration, cache FROM caches WHERE id = :id LIMIT 0, 1');
// Try and load the cache based on id
try
@@ -126,7 +135,7 @@ public function set($id, $data, $lifetime = NULL)
public function delete($id)
{
// Prepare statement
- $statement = $this->_db->prepare('DELETE FROM caches'.$this->_config['suffix'].' WHERE id = :id');
+ $statement = $this->_db->prepare('DELETE FROM caches WHERE id = :id');
// Remove the entry
try
@@ -149,7 +158,7 @@ public function delete($id)
public function delete_all()
{
// Prepare statement
- $statement = $this->_db->prepare('DELETE FROM caches'.$this->_config['suffix'].'');
+ $statement = $this->_db->prepare('DELETE FROM caches');
// Remove the entry
try
@@ -185,16 +194,16 @@ public function set_with_tags($id, $data, $lifetime = NULL, array $tags = NULL)
// Setup lifetime
if ($lifetime === NULL)
{
- $lifetime = (0 === Arr::get('default_expire', NULL)) ? 0 : (Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE) + time());
+ $lifetime = time() + Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE);
}
else
{
- $lifetime = (0 === $lifetime) ? 0 : ($lifetime + time());
+ $lifetime = ($lifetime === 0) ? 0 : (time() + $lifetime);
}
// Prepare statement
// $this->exists() may throw Cache_Exception, no need to catch/rethrow
- $statement = $this->exists($id) ? $this->_db->prepare('UPDATE caches'.$this->_config['suffix'].' SET expiration = :expiration, cache = :cache, tags = :tags WHERE id = :id') : $this->_db->prepare('INSERT INTO caches'.$this->_config['suffix'].' (id, cache, expiration, tags) VALUES (:id, :cache, :expiration, :tags)');
+ $statement = $this->exists($id) ? $this->_db->prepare('UPDATE caches SET expiration = :expiration, cache = :cache, tags = :tags WHERE id = :id') : $this->_db->prepare('INSERT INTO caches (id, cache, expiration, tags) VALUES (:id, :cache, :expiration, :tags)');
// Try to insert
try
@@ -220,7 +229,7 @@ public function set_with_tags($id, $data, $lifetime = NULL, array $tags = NULL)
public function delete_tag($tag)
{
// Prepare the statement
- $statement = $this->_db->prepare('DELETE FROM caches'.$this->_config['suffix'].' WHERE tags LIKE :tag');
+ $statement = $this->_db->prepare('DELETE FROM caches WHERE tags LIKE :tag');
// Try to delete
try
@@ -245,7 +254,7 @@ public function delete_tag($tag)
public function find($tag)
{
// Prepare the statement
- $statement = $this->_db->prepare('SELECT id, cache FROM caches'.$this->_config['suffix'].' WHERE tags LIKE :tag');
+ $statement = $this->_db->prepare('SELECT id, cache FROM caches WHERE tags LIKE :tag');
// Try to find
try
@@ -285,7 +294,7 @@ public function find($tag)
public function garbage_collect()
{
// Create the sequel statement
- $statement = $this->_db->prepare('DELETE FROM caches'.$this->_config['suffix'].' WHERE expiration < :expiration');
+ $statement = $this->_db->prepare('DELETE FROM caches WHERE expiration < :expiration');
try
{
@@ -306,7 +315,7 @@ public function garbage_collect()
*/
protected function exists($id)
{
- $statement = $this->_db->prepare('SELECT id FROM caches'.$this->_config['suffix'].' WHERE id = :id');
+ $statement = $this->_db->prepare('SELECT id FROM caches WHERE id = :id');
try
{
$statement->execute(array(':id' => $this->_sanitize_id($id)));
View
4 classes/caching/jelly/builder.php
@@ -221,11 +221,11 @@ public function count($db = NULL)
if($this->_config->available)
{
// Extract cached data if it exists
- $result = Cache::instance()->get($id);
+ $result = $this->_cache->get($id, NULL);
}
// Make cache routine if result is empty
- if( ! $result)
+ if($result === NULL)
{
$model = ($this->_meta) ? $this->_meta->model() : $this->_model;

0 comments on commit c19409c

Please sign in to comment.
Something went wrong with that request. Please try again.