Skip to content

Commit

Permalink
Merge pull request jpfuentes2#2 from shmax/fix-up-wtf
Browse files Browse the repository at this point in the history
Fix up wtf
  • Loading branch information
shmax committed Jun 29, 2014
2 parents a168548 + 84d5fb0 commit 4f62e44
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
15 changes: 13 additions & 2 deletions lib/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ public static function get($key, $closure)
{
$key = static::get_namespace() . $key;

if (!static::$adapter)
if (!static::$adapter){
return $closure();
}

if (!($value = static::$adapter->read($key))){
static::$adapter->write($key,($value = $closure()),static::$options['expire']);
static::$adapter->set($key,($value = $closure()),static::$options['expire']);
}

return $value;
Expand All @@ -77,9 +78,19 @@ public static function set($key, $var, $expire=0){
if (!static::$adapter)
return;

$key = static::get_namespace() . $key;
return static::$adapter->write($key, $var, $expire);
}

public static function delete($key){
if (!static::$adapter)
return;

$key = static::get_namespace() . $key;

return static::$adapter->delete($key);
}

private static function get_namespace()
{
return (isset(static::$options['namespace']) && strlen(static::$options['namespace']) > 0) ? (static::$options['namespace'] . "::") : "";
Expand Down
53 changes: 45 additions & 8 deletions lib/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,6 @@ private function update($validate=true)

if ($this->is_dirty())
{
$table = static::table();
$pk = $this->values_for_pk();

if (empty($pk))
Expand All @@ -870,17 +869,26 @@ private function update($validate=true)
$dirty = $this->dirty_attributes();
static::table()->update($dirty,$pk);

if($table->cache_model){
$key = static::Table()->cache_key_for_model($this->attributes());
Cache::set($key, $this, 0);
}
$this->update_cache();

$this->invoke_callback('after_update',false);
}

return true;
}

protected function update_cache(){
$table = static::table();
if($table->cache_model){
Cache::set($this->cache_key(), $this, 0);
}
}

protected function cache_key(){
$table = static::Table();
return $table->cache_key_for_model($this->values_for_pk());
}

/**
* Deletes records matching conditions in $options
*
Expand Down Expand Up @@ -936,6 +944,7 @@ public static function delete_all($options=array())

$values = $sql->bind_values();
$ret = $conn->query(($table->last_sql = $sql->to_s()), $values);

return $ret->rowCount();
}

Expand Down Expand Up @@ -1015,10 +1024,19 @@ public function delete()

static::table()->delete($pk);
$this->invoke_callback('after_destroy',false);
$this->remove_from_cache();

return true;
}


public function remove_from_cache(){
$table = static::table();
if($table->cache_model){
Cache::delete($this->cache_key());
}
}

/**
* Helper that creates an array of values for the primary key(s).
*
Expand Down Expand Up @@ -1252,7 +1270,10 @@ public function set_relationship_from_eager_load(Model $model=null, $name)
*/
public function reload()
{
$this->remove_from_cache();

$this->__relationships = array();

$pk = array_values($this->get_values_for($this->get_primary_key()));

$this->set_attributes_via_mass_assignment($this->find($pk)->attributes, false);
Expand Down Expand Up @@ -1573,8 +1594,9 @@ public static function find(/* $type, $options */)
$args = $args[0];

// anything left in $args is a find by pk
if ($num_args > 0 && !isset($options['conditions']))
if ($num_args > 0 && !isset($options['conditions'])){
return static::find_by_pk($args, $options);
}

$options['mapped_names'] = static::$alias_attribute;
$list = static::table()->find($options);
Expand All @@ -1593,8 +1615,23 @@ public static function find(/* $type, $options */)
*/
public static function find_by_pk($values, $options)
{
$options['conditions'] = static::pk_conditions($values);
$list = static::table()->find($options);
$table = static::table();

if($table->cache_model){
$pks=is_array($values)?$values:array($values);
foreach($pks as $pk){
$options['conditions'] = static::pk_conditions($pk);
$list[] = Cache::get($table->cache_key_for_model($pk), function() use ($table, $options){
$res = $table->find($options);
return $res?$res[0]:null;
});
}
$list = array_filter($list);
}
else{
$options['conditions'] = static::pk_conditions($values);
$list = $table->find($options);
}
$results = count($list);

if ($results != ($expected = count($values)))
Expand Down
9 changes: 6 additions & 3 deletions lib/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,11 @@ public function find($options)
return $this->find_by_sql($sql->to_s(),$sql->get_where_values(), $readonly, $eager_load);
}

public function cache_key_for_model(array $attributes){
return $this->class->name."-".implode("-",array_intersect_key($attributes, array_flip($this->pk)));
public function cache_key_for_model($pk){
if(!is_array($pk)){
$pk = array($pk);
}
return $this->class->name."-".implode("-",$pk);
}

public function find_by_sql($sql, $values=null, $readonly=false, $includes=null)
Expand All @@ -234,7 +237,7 @@ public function find_by_sql($sql, $values=null, $readonly=false, $includes=null)
return new $self->class->name($row,false,true,false);
};
if($this->cache_model){
$key = $this->cache_key_for_model($row);
$key = $this->cache_key_for_model(array_intersect_key($row, array_flip($this->pk)));
$model = Cache::get($key, $cb );
}
else{
Expand Down

0 comments on commit 4f62e44

Please sign in to comment.