Skip to content

Commit 0aea1ff

Browse files
committed
Adding database caching
1 parent 2d8449e commit 0aea1ff

File tree

7 files changed

+94
-12
lines changed

7 files changed

+94
-12
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ services: mongodb
1313

1414
before_script:
1515
- echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
16-
- composer install --no-interaction
16+
- composer install --dev --no-interaction
1717

1818
script: phpunit tests

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Tell your model to use the MongoDB model and set the collection (alias for table
6060

6161
}
6262

63-
**Everything else works just like the original Eloquent model.**
63+
Everything else works just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent
6464

6565
Query Builder
6666
-------------
@@ -176,4 +176,8 @@ You may also specify additional columns to update:
176176
User::where('age', '29')->increment('age', 1, array('group' => 'thirty something'));
177177
User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight'));
178178

179-
Read more about the Eloquent on http://laravel.com/docs/eloquent
179+
**Query Caching**
180+
181+
You may easily cache the results of a query using the remember method:
182+
183+
$users = User::remember(10)->get();

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"illuminate/database": "4.1.x",
1616
"illuminate/events": "4.1.x"
1717
},
18+
"require-dev": {
19+
"illuminate/cache": "4.1.x"
20+
},
1821
"autoload": {
1922
"psr-0": {
2023
"Jenssegers\\Mongodb": "src/"

src/Jenssegers/Mongodb/Builder.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,17 @@ public function find($id, $columns = array('*'))
5151
}
5252

5353
/**
54-
* Execute the query as a "select" statement.
54+
* Execute the query as a fresh "select" statement.
5555
*
5656
* @param array $columns
57-
* @return array
57+
* @return array|static[]
5858
*/
59-
public function get($columns = array('*'))
59+
public function getFresh($columns = array('*'))
6060
{
6161
// If no columns have been specified for the select statement, we will set them
6262
// here to either the passed columns, or the standard default of retrieving
6363
// all of the columns on the table using the "wildcard" column character.
64-
if (is_null($this->columns))
65-
{
66-
$this->columns = $columns;
67-
}
64+
if (is_null($this->columns)) $this->columns = $columns;
6865

6966
// Drop all columns if * is present
7067
if (in_array('*', $this->columns))
@@ -160,6 +157,27 @@ public function get($columns = array('*'))
160157
}
161158
}
162159

160+
/**
161+
* Generate the unique cache key for the query.
162+
*
163+
* @return string
164+
*/
165+
public function generateCacheKey()
166+
{
167+
$key = array(
168+
'connection' => $this->connection->getName(),
169+
'collection' => $this->collection->getName(),
170+
'wheres' => $this->wheres,
171+
'columns' => $this->columns,
172+
'groups' => $this->groups,
173+
'orders' => $this->orders,
174+
'offset' => $this->offset,
175+
'aggregate' => $this->aggregate,
176+
);
177+
178+
return md5(serialize(array_values($key)));
179+
}
180+
163181
/**
164182
* Execute an aggregate function on the database.
165183
*
@@ -173,6 +191,11 @@ public function aggregate($function, $columns = array('*'))
173191

174192
$results = $this->get($columns);
175193

194+
// Once we have executed the query, we will reset the aggregate property so
195+
// that more select queries can be executed against the database without
196+
// the aggregate value getting in the way when the grammar builds it.
197+
$this->columns = null; $this->aggregate = null;
198+
176199
if (isset($results[0]))
177200
{
178201
return $results[0][$columns[0]];
@@ -343,7 +366,7 @@ public function decrement($column, $amount = 1, array $extra = array())
343366
*/
344367
public function delete($id = null)
345368
{
346-
$query = $this->compileWheres($this);
369+
$query = $this->compileWheres();
347370
$result = $this->collection->remove($query);
348371

349372
if (1 == (int) $result['ok'])

tests/CacheTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
require_once('vendor/autoload.php');
3+
require_once('models/User.php');
4+
5+
use Jenssegers\Mongodb\Facades\DB;
6+
7+
class CacheTest extends PHPUnit_Framework_TestCase {
8+
9+
protected $app;
10+
11+
public function setUp()
12+
{
13+
include('tests/app.php');
14+
$this->app = $app;
15+
16+
// test data
17+
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
18+
User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin'));
19+
User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user'));
20+
}
21+
22+
public function tearDown()
23+
{
24+
User::truncate();
25+
}
26+
27+
public function testCache()
28+
{
29+
# auto generate cache key
30+
$users = DB::collection('users')->where('age', '>', 10)->remember(10)->get();
31+
$this->assertEquals(3, count($users));
32+
33+
$users = DB::collection('users')->where('age', '>', 10)->getCached();
34+
$this->assertEquals(3, count($users));
35+
36+
# store under predefined cache key
37+
$users = User::where('age', '>', 10)->remember(10, 'db.users')->get();
38+
$this->assertEquals(3, count($users));
39+
40+
# get from cache driver
41+
$cache = $this->app['cache'];
42+
$users = $cache->get('db.users');
43+
$this->assertEquals(3, count($users));
44+
}
45+
46+
}

tests/QueryTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22
require_once('vendor/autoload.php');
3-
require_once('models/User.php');
43

54
use Jenssegers\Mongodb\Facades\DB;
65

tests/app.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44
use Jenssegers\Mongodb\DatabaseManager;
55
use Jenssegers\Mongodb\Facades\DB;
66
use Illuminate\Events\Dispatcher;
7+
use Illuminate\Cache\ArrayStore;
8+
use Illuminate\Cache\Repository;
79

810
# Fake app
911
$app = array();
1012

13+
# Event dispatcher
1114
$app['events'] = new Dispatcher;
1215

16+
# Cache driver
17+
$app['cache'] = new Repository(new ArrayStore);
18+
1319
# Database configuration
1420
$app['config']['database.connections']['mongodb'] = array(
21+
'name' => 'mongodb',
1522
'host' => 'localhost',
1623
'database' => 'unittest'
1724
);

0 commit comments

Comments
 (0)