Skip to content

Instantly share code, notes, and snippets.

@phalcon
Created January 31, 2013 08:01
Show Gist options
  • Select an option

  • Save phalcon/4681167 to your computer and use it in GitHub Desktop.

Select an option

Save phalcon/4681167 to your computer and use it in GitHub Desktop.
<?php
class CacheModel extends Phalcon\Mvc\Model
{
static protected $_cacheKey;
static protected $_cacheLifetime = 3600;
public static function getCacheKey($parameters)
{
if (isset($parameters['di'])) {
unset ($parameters['di']);
}
return get_called_class() . md5(serialize($parameters));
}
public static function findFirst($parameters = null)
{
if ($parameters) {
$di = \Phalcon\DI::getDefault();
$cache = $di->getCache();
$key = self::getCacheKey($parameters);
$record = $cache->get($key);
if ($record===null) {
$record = parent::findFirst($parameters);
$cache->save($key, $record, self::$_cacheLifetime);
} else {
$di->getModelsManager()->initialize($record);
}
} else {
$record = parent::findFirst($parameters);
}
return $record;
}
public static function find($parameters = null)
{
if ($parameters) {
$di = \Phalcon\DI::getDefault();
$cache = $di->getCache();
$key = self::getCacheKey($parameters);
$record = $cache->get($key);
if ($record===null) {
$record = parent::find($parameters);
$cache->save($key, $record, self::$_cacheLifetime);
} else {
$di->getModelsManager()->initialize($record);
}
} else {
$record = parent::find($parameters);
}
return $record;
}
}
class Robots extends CacheModel
{
public function initialize()
{
$this->hasMany('id', 'RobotsParts', 'robots_id');
}
}
class Parts extends CacheModel
{
public function initialize()
{
$this->hasMany('id', 'RobotsParts', 'robots_id');
}
}
class RobotsParts extends CacheModel
{
public function initialize()
{
$this->belongsTo('robots_id', 'Robots', 'id');
$this->belongsTo('parts_id', 'Parts', 'id');
}
}
$di = new \Phalcon\DI\FactoryDefault();
$ev = new Phalcon\Events\Manager();
$connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'phalcon_test'
));
$ev->attach('db', function($event, $connection){
if ($event->getType() == 'beforeQuery') {
echo $connection->getSqlStatement(), PHP_EOL;
}
});
$connection->setEventsManager($ev);
$di['db'] = $connection;
$di['cache'] = function() {
//Cache data for one day by default
$frontCache = new \Phalcon\Cache\Frontend\Data(array(
"lifetime" => 86400
));
//Memcached connection settings
$cache = new \Phalcon\Cache\Backend\File($frontCache, array(
"cacheDir" => "./unit-tests/cache/",
));
return $cache;
};
$robot = Robots::findFirst(1);
if (!is_object($robot)) {
die ('no robot');
}
$robotsParts = $robot->getRobotsParts();
if (!is_object($robotsParts)) {
die ('no robot');
}
$robot = Robots::findFirst(1);
if (!is_object($robot)) {
die ('no robot');
}
$robot = Robots::findFirst(2);
if (!is_object($robot)) {
die ('no robot');
}
$robot = Robots::findFirst(3);
if (!is_object($robot)) {
die ('no robot');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment