Skip to content
This repository was archived by the owner on May 12, 2022. It is now read-only.

Commit 43d9fa7

Browse files
author
Nik Barham
committed
Adding cache clause to Model::find
1 parent d76c1e6 commit 43d9fa7

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/Automatorm/Orm/Model.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,39 @@ public static function get($id, $force_refresh = false)
3434
}
3535

3636
// Find a single(!) object via an arbitary $where clause
37-
public static function find($where)
37+
public static function find($where, $cacheable = false)
3838
{
39-
$o = new Core\QueryOptions();
40-
return static::factory($where, null, null, $o->limit(1), true);
39+
// If result is cacheable, look in the cache
40+
if ($cacheable)
41+
{
42+
// Get table/class data
43+
if (!$database) $database = static::$dbconnection;
44+
$schema = Schema::get($database);
45+
list($class, $table) = $schema->guessContext($class_or_table ?: get_called_class());
46+
47+
// Hash where clause
48+
$key = md5(serialize($where));
49+
50+
// Look in cache
51+
if (isset(Model::$instance[$database][$table][$key])) {
52+
$result = Model::$instance[$database][$table][$key];
53+
}
54+
}
55+
56+
// No cache or cache miss
57+
if (!$result)
58+
{
59+
$o = new Core\QueryOptions();
60+
$result = static::factory($where, null, null, $o->limit(1), true);
61+
62+
// If caching, store in cache
63+
if ($cacheable)
64+
{
65+
Model::$instance[$database][$table][$key] = $result;
66+
}
67+
}
68+
69+
return $result;
4170
}
4271

4372
// Find a collection of objects via an arbitary $where clause

0 commit comments

Comments
 (0)