Skip to content

Commit

Permalink
replaced old model constructor with new find() method that looks up m…
Browse files Browse the repository at this point in the history
…odels by a given ID + model constructor now accepts an array of values + caching now happens in find() instead of refresh() + remove deprecated load() method + throw exception when a model driver has not been set
  • Loading branch information
Jared King committed Dec 28, 2015
1 parent f963613 commit 4ac3f53
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 320 deletions.
62 changes: 30 additions & 32 deletions src/Cacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,42 @@ trait Cacheable
*/
private $_cacheItem;

public function refresh()
public static function find($id)
{
if ($this->_id === false) {
return $this;
}

if (self::$cachePool) {
// First, attempts to load the model from the caching layer.
// If that fails, then attempts to load the model from the
// database layer.
$item = $this->getCacheItem();
// Attempt to load the model from the caching layer first.
// If that fails, then fall through to the data layer.
$model = static::buildFromId($id);
$item = $model->getCacheItem();
$values = $item->get();

if ($item->isMiss()) {
// If the cache was a miss, then lock down the
// cache item, attempt to refresh the model from
// the database, and then update the cache.
// Stash calls this Stampede Protection.

// NOTE Currently disabling Stampede Protection
// because there is no way to unlock the item
// if we fail to refresh the model, whether
// due to a DB failure or non-existent record.
// This is problematic with the Redis driver
// because it will attempt to unlock the cache
// item once the script shuts down and the
// redis connection has closed.
// $item->lock();

parent::refresh();
} else {
$this->_values = $values;
if (!$item->isMiss()) {
// load the values directly instead of using
// refreshWith() to prevent triggering another
// cache call
$model->_exists = true;
$model->_values = $values;

return $model;
}
} else {
parent::refresh();

// If the cache was a miss, then lock down the
// cache item, attempt to load the model from
// the database, and then update the cache.
// Stash calls this Stampede Protection.

// NOTE Currently disabling Stampede Protection
// because there is no way to unlock the item
// if we fail to load the model, whether
// due to a DB failure or non-existent record.
// This is problematic with the Redis driver
// because it will attempt to unlock the cache
// item once the script shuts down and the
// redis connection has closed.
// $item->lock();
}

return $this;
return parent::find($id);
}

public function refreshWith(array $values)
Expand Down Expand Up @@ -125,7 +123,7 @@ public function getCacheKey()
self::$cachePrefix[$k] = 'models/'.strtolower(static::modelName());
}

return self::$cachePrefix[$k].'/'.$this->_id;
return self::$cachePrefix[$k].'/'.$this->id();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Driver/DriverInterface.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

/**
* @package Pulsar
* @author Jared King <j@jaredtking.com>
*
* @link http://jaredtking.com
*
* @copyright 2015 Jared King
* @license MIT
*/

namespace Pulsar\Driver;

use Pulsar\Model;
Expand Down Expand Up @@ -41,7 +41,7 @@ public function getCreatedID(Model $model, $propertyName);
*
* @param \Pulsar\Model $model
*
* @return array
* @return array|false
*/
public function loadModel(Model $model);

Expand Down

0 comments on commit 4ac3f53

Please sign in to comment.