Author: develtainment Date: 2010-03-12 15:26:50 +0100 (Fri, 12 Mar 2010) New Revision: 28501
Added: plugins/sfPropelCachePlugin/package.xml plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/ plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/LICENSE plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/README plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/config/ plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/config/databases.yml.sample plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/config/factories.yml.sample plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/data/ plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/data/sql/ plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/data/sql/sfPropelCache.MySQL.schema.sql plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/lib/ plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/lib/sfPropelCache.class.php Log: initial Added: plugins/sfPropelCachePlugin/package.xml =================================================================== --- plugins/sfPropelCachePlugin/package.xml (rev 0) +++ plugins/sfPropelCachePlugin/package.xml 2010-03-12 14:26:50 UTC (rev 28501) @@ -0,0 +1,74 @@ +<?xml version="1.0" encoding="UTF-8"?> +<package packagerversion="1.9.0" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> + <name>sfPropelCachePlugin</name> + <channel>plugins.symfony-project.org</channel> + <summary>Propel cache plugin.</summary> + <description>Use a Propel connection for symfony caching.</description> + <lead> + <name>Tobias Sülzenbrück</name> + <user>develtainment</user> + <email>[email protected]</email> + <active>yes</active> + </lead> + <developer> + <name>Klaus Großmann</name> + <user>iKlaus</user> + <email>[email protected]</email> + <active>yes</active> + </developer> + <date>2010-03-12</date> + <time>14:06:32</time> + <version> + <release>1.0.0</release> + <api>1.0.0</api> + </version> + <stability> + <release>stable</release> + <api>stable</api> + </stability> + <license uri="http://www.symfony-project.com/license">MIT license</license> + <notes>-</notes> + <contents> + <dir name="/"> + <file md5sum="29e598952d315a7c2e34c13090ad92d2" name="config/databases.yml.sample" role="data" /> + <file md5sum="67a9561fe616155a731f9c8b4f56ef8b" name="config/factories.yml.sample" role="data" /> + <file md5sum="848b175fedcd09cf586f5ba3e5139cd8" name="data/sql/sfPropelCache.MySQL.schema.sql" role="data" /> + <file md5sum="1045339322f4d254d2189b91752f9600" name="lib/sfPropelCache.class.php" role="data" /> + <file md5sum="ba74ac9fa90f055d91381bc1ec629847" name="README" role="data" /> + <file md5sum="813f42ca4ab06c15dcbadef6a88755ac" name="LICENSE" role="data" /> + </dir> + </contents> + <dependencies> + <required> + <php> + <min>5.2.4</min> + </php> + <pearinstaller> + <min>1.4.1</min> + </pearinstaller> + <package> + <name>symfony</name> + <channel>pear.symfony-project.com</channel> + <min>1.4.1</min> + <max>1.4.3</max> + </package> + </required> + </dependencies> + <phprelease /> + <changelog> + <release> + <version> + <release>1.0.0</release> + <api>1.0.0</api> + </version> + <stability> + <release>stable</release> + <api>stable</api> + </stability> + <license uri="http://www.symfony-project.com/license">MIT license</license> + <license>MIT</license> + <date>2009-03-12</date> + <notes>* initial release</notes> + </release> + </changelog> +</package> Added: plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/LICENSE =================================================================== --- plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/LICENSE (rev 0) +++ plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/LICENSE 2010-03-12 14:26:50 UTC (rev 28501) @@ -0,0 +1,7 @@ +Copyright (c) 2010 Tobias Sülzenbrück + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file Added: plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/README =================================================================== --- plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/README (rev 0) +++ plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/README 2010-03-12 14:26:50 UTC (rev 28501) @@ -0,0 +1,34 @@ +sfPropelCache Plugin +==================== + +Installation +------------ + + * Install the plugin + + $ symfony plugin:install sfPropelCachePlugin + + * Setup sfPropelCache as view cache in your `factories.yml` (see `config/factories.yml.sample`): + + [yml] + all: + view_cache: + class: sfPropelCache + param: + automatic_cleaning_factor: 10 + + * Add the database connection parameters to your `databases.yml` (see `config/databases.yml.sample`): + + [yml] + cache_db_connection: + dsn: mysql:dbname=your_dbname;host=localhost + username: your_username + password: your_password + + all: + ... + + * Add the cache table (`dt_cache`) to the indicated database. For MySQL you may use the provided sql file (see `data/sql/sfPropelCache.MySQL.schema.sql`). + + + \ No newline at end of file Added: plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/config/databases.yml.sample =================================================================== --- plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/config/databases.yml.sample (rev 0) +++ plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/config/databases.yml.sample 2010-03-12 14:26:50 UTC (rev 28501) @@ -0,0 +1,4 @@ +cache_db_connection: + dsn: mysql:dbname=your_dbname;host=localhost + username: your_username + password: your_password Added: plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/config/factories.yml.sample =================================================================== --- plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/config/factories.yml.sample (rev 0) +++ plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/config/factories.yml.sample 2010-03-12 14:26:50 UTC (rev 28501) @@ -0,0 +1,5 @@ +all: + view_cache: + class: sfMySQLCache + param: + automatic_cleaning_factor: 10 \ No newline at end of file Added: plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/data/sql/sfPropelCache.MySQL.schema.sql =================================================================== --- plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/data/sql/sfPropelCache.MySQL.schema.sql (rev 0) +++ plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/data/sql/sfPropelCache.MySQL.schema.sql 2010-03-12 14:26:50 UTC (rev 28501) @@ -0,0 +1,25 @@ +# ############################################### +# Table definition for sfPropelCachePlugin +# by Tobias Sülzenbrück <[email protected] +# ############################################### + +SET FOREIGN_KEY_CHECKS = 0; + +#------------------------------------------------- +#-- dt_cache +#------------------------------------------------- + +DROP TABLE IF EXISTS `dt_cache`; + +CREATE TABLE `dt_cache` +( + `key` varchar(255) NOT NULL, + `data` longtext NOT NULL, + `timeout` int(11) NOT NULL DEFAULT '0', + `last_modified` int(11) NOT NULL DEFAULT '0', + UNIQUE KEY `cache_unique_key` (`key`), + KEY `idx_timeout` (`timeout`), + KEY `idx_last_modified` (`last_modified`) +)ENGINE=InnoDB; + +SET FOREIGN_KEY_CHECKS = 1; Added: plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/lib/sfPropelCache.class.php =================================================================== --- plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/lib/sfPropelCache.class.php (rev 0) +++ plugins/sfPropelCachePlugin/sfPropelCachePlugin-1.0.0/lib/sfPropelCache.class.php 2010-03-12 14:26:50 UTC (rev 28501) @@ -0,0 +1,219 @@ +<?php +/** + * sfPropelCache. + * + * @package develtainment + * @subpackage sfPropelCache + * @author Tobias Sülzenbrück <[email protected]> + * @author Klaus Großmann <[email protected]> + * @version SVN: $Id: sfPropelCache.class.php 213 2010-03-11 23:24:16Z klaus $ + */ + +class sfPropelCache extends sfCache +{ + protected $con = null; + protected $config_name = 'cache_db_connection'; + protected $table_name = 'dt_cache'; + protected $col_key = 'key'; + protected $col_data = 'data'; + protected $col_timeout = 'timeout'; + protected $col_last_modified = 'last_modified'; + + /** + * Initializes this sfCache instance. + * + * Available options: + * + * * database: File where to put the cache database (or :memory: to store cache in memory) + * + * * see sfCache for options available for all drivers + * + * @see sfCache + */ + public function initialize($options = array()) + { + // disable auto_start + $options['auto_start'] = false; + + // initialize the parent + parent::initialize($options); + + $config = sfYaml::load(sfConfig::get('sf_config_dir').'/databases.yml'); + + if (!isset($config[$this->config_name])) + { + throw new sfInitializationException('You must provide a "'.$this->config_name.'" information in databases.yml to sfPropelCache.'); + } + + $cache_db_connection = $config[$this->config_name]; + + if (!isset($cache_db_connection['dsn'])) + { + throw new sfInitializationException('You must provide a "dsn" option to sfPropelCache.'); + } + + if (!isset($cache_db_connection['username'])) + { + throw new sfInitializationException('You must provide a "username" option to sfPropelCache.'); + } + + if (!isset($cache_db_connection['password'])) + { + throw new sfInitializationException('You must provide a "password" option to sfPropelCache.'); + } + + $this->con = new PDO( $cache_db_connection['dsn'], $cache_db_connection['username'], $cache_db_connection['password']); + } + + /** + * @see sfCache + */ + public function getHash($key) + { + return $key; + } + + /** + * @see sfCache + */ + public function getBackend() + { + return $this->con; + } + + /** + * @see sfCache + */ + public function get($key, $default = null) + { + $stmt = $this->con->prepare('SELECT `'.$this->col_data.'` FROM `'.$this->table_name.'` WHERE `'.$this->col_key.'` = :key LIMIT 0, 1 '); + $stmt->bindValue(':key', self::getHash($key), PDO::PARAM_STR); + $stmt->execute(); + + $resultset = $stmt->fetch(PDO::FETCH_ASSOC); + + $data = $resultset[$this->col_data]; + return null === $data ? $default : $data; + } + + /** + * @see sfCache + */ + public function has($key) + { + $stmt = $this->con->prepare('SELECT `'.$this->col_key.'` FROM `'.$this->table_name.'` WHERE `'.$this->col_key.'` = :key LIMIT 0, 1 '); + $stmt->bindValue(':key', self::getHash($key), PDO::PARAM_STR); + $stmt->execute(); + + $resultset = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($resultset) { + return true; + }else { + return false; + } + } + + /** + * @see sfCache + */ + public function set($key, $data, $lifetime = null) + { + if ($this->getOption('automatic_cleaning_factor') > 0 && rand(1, $this->getOption('automatic_cleaning_factor')) == 1) + { + $this->clean(sfCache::OLD); + } + + $stmt = $this->con->prepare('INSERT INTO `'.$this->table_name.'` (`'.$this->col_key.'`, `'.$this->col_data.'`, `'.$this->col_timeout.'`, `'.$this->col_last_modified.'`) VALUES (:key, :data, :timeout, :last_modified) ON DUPLICATE KEY UPDATE `'.$this->col_key.'` = :key, `'.$this->col_data.'` = :data, `'.$this->col_timeout.'` = :timeout, `'.$this->col_last_modified.'` = :last_modified '); + $stmt->bindValue(':key', self::getHash($key), PDO::PARAM_STR); + $stmt->bindValue(':data', $data, PDO::PARAM_STR); + $stmt->bindValue(':timeout', (time() + $this->getLifetime($lifetime)), PDO::PARAM_INT); + $stmt->bindValue(':last_modified', time(), PDO::PARAM_INT); + + return (boolean) $stmt->execute(); + } + + /** + * @see sfCache + */ + public function remove($key) + { + $stmt = $this->con->prepare('DELETE FROM `'.$this->table_name.'` WHERE `'.$this->col_key.'` = :key LIMIT 0, 1 '); + $stmt->bindValue(':key', self::getHash($key), PDO::PARAM_STR); + + return (boolean) $stmt->execute(); + } + + /** + * @see sfCache + */ + public function removePattern($pattern) + { + $stmt = $this->con->prepare('DELETE FROM `'.$this->table_name.'` WHERE REGEXP(:pattern, `'.$this->col_key.'`)'); + $stmt->bindValue(':pattern', self::patternToRegexp($pattern), PDO::PARAM_STR); + + return (boolean) $stmt->execute(); + } + + /** + * @see sfCache + */ + public function clean($mode = sfCache::ALL) + { + if (sfCache::OLD == $mode) { + $stmt = $this->con->prepare('DELETE FROM `'.$this->table_name.'` WHERE `'.$this->col_timeout.'` < :timeout '); + $stmt->bindValue(':timeout', time(), PDO::PARAM_INT); + }else { + $stmt = $this->con->prepare('TRUNCATE TABLE `'.$this->table_name.'` '); + } + + return (boolean) $stmt->execute(); + } + + /** + * @see sfCache + */ + public function getTimeout($key) + { + $stmt = $this->con->prepare('SELECT `'.$this->col_timeout.'` FROM `'.$this->table_name.'` WHERE `'.$this->col_key.'` = :key AND `'.$this->col_timeout.'` > :timeout LIMIT 0, 1 '); + $stmt->bindValue(':key', self::getHash($key), PDO::PARAM_STR); + $stmt->bindValue(':timeout', time(), PDO::PARAM_INT); + $stmt->execute(); + + $resultset = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($resultset && isset($resultset[$this->col_timeout])) { + return intval($resultset[$this->col_timeout]); + }else { + return 0; + } + } + + /** + * @see sfCache + */ + public function getLastModified($key) + { + $stmt = $this->con->prepare('SELECT `'.$this->col_last_modified.'` FROM `'.$this->table_name.'` WHERE `'.$this->col_key.'` = :key AND `'.$this->col_timeout.'` > :timeout LIMIT 0, 1 '); + $stmt->bindValue(':key', self::getHash($key), PDO::PARAM_STR); + $stmt->bindValue(':timeout', time(), PDO::PARAM_INT); + $stmt->execute(); + + $resultset = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($resultset && isset($resultset[$this->col_last_modified])) { + return intval($resultset[$this->col_last_modified]); + }else { + return 0; + } + } + + /** + * Callback used when deleting keys from cache. + */ + public function removePatternRegexpCallback($regexp, $key) + { + return preg_match($regexp, $key); + } + +} \ No newline at end of file -- You received this message because you are subscribed to the Google Groups "symfony SVN" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/symfony-svn?hl=en.
