Commit 3f1be668 authored by Jens Segers's avatar Jens Segers

Adding database caching

parent f2964d41
...@@ -13,6 +13,6 @@ services: mongodb ...@@ -13,6 +13,6 @@ services: mongodb
before_script: before_script:
- echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini - echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
- composer install --no-interaction - composer install --dev --no-interaction
script: phpunit tests script: phpunit tests
\ No newline at end of file
...@@ -60,7 +60,7 @@ Tell your model to use the MongoDB model and set the collection (alias for table ...@@ -60,7 +60,7 @@ Tell your model to use the MongoDB model and set the collection (alias for table
} }
**Everything else works just like the original Eloquent model.** Everything else works just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent
Query Builder Query Builder
------------- -------------
...@@ -176,4 +176,8 @@ You may also specify additional columns to update: ...@@ -176,4 +176,8 @@ You may also specify additional columns to update:
User::where('age', '29')->increment('age', 1, array('group' => 'thirty something')); User::where('age', '29')->increment('age', 1, array('group' => 'thirty something'));
User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight')); User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight'));
Read more about the Eloquent on http://laravel.com/docs/eloquent **Query Caching**
\ No newline at end of file
You may easily cache the results of a query using the remember method:
$users = User::remember(10)->get();
\ No newline at end of file
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
"illuminate/database": "4.1.x", "illuminate/database": "4.1.x",
"illuminate/events": "4.1.x" "illuminate/events": "4.1.x"
}, },
"require-dev": {
"illuminate/cache": "4.1.x"
},
"autoload": { "autoload": {
"psr-0": { "psr-0": {
"Jenssegers\\Mongodb": "src/" "Jenssegers\\Mongodb": "src/"
......
...@@ -51,20 +51,17 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -51,20 +51,17 @@ class Builder extends \Illuminate\Database\Query\Builder {
} }
/** /**
* Execute the query as a "select" statement. * Execute the query as a fresh "select" statement.
* *
* @param array $columns * @param array $columns
* @return array * @return array|static[]
*/ */
public function get($columns = array('*')) public function getFresh($columns = array('*'))
{ {
// If no columns have been specified for the select statement, we will set them // If no columns have been specified for the select statement, we will set them
// here to either the passed columns, or the standard default of retrieving // here to either the passed columns, or the standard default of retrieving
// all of the columns on the table using the "wildcard" column character. // all of the columns on the table using the "wildcard" column character.
if (is_null($this->columns)) if (is_null($this->columns)) $this->columns = $columns;
{
$this->columns = $columns;
}
// Drop all columns if * is present // Drop all columns if * is present
if (in_array('*', $this->columns)) if (in_array('*', $this->columns))
...@@ -160,6 +157,27 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -160,6 +157,27 @@ class Builder extends \Illuminate\Database\Query\Builder {
} }
} }
/**
* Generate the unique cache key for the query.
*
* @return string
*/
public function generateCacheKey()
{
$key = array(
'connection' => $this->connection->getName(),
'collection' => $this->collection->getName(),
'wheres' => $this->wheres,
'columns' => $this->columns,
'groups' => $this->groups,
'orders' => $this->orders,
'offset' => $this->offset,
'aggregate' => $this->aggregate,
);
return md5(serialize(array_values($key)));
}
/** /**
* Execute an aggregate function on the database. * Execute an aggregate function on the database.
* *
...@@ -173,6 +191,11 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -173,6 +191,11 @@ class Builder extends \Illuminate\Database\Query\Builder {
$results = $this->get($columns); $results = $this->get($columns);
// Once we have executed the query, we will reset the aggregate property so
// that more select queries can be executed against the database without
// the aggregate value getting in the way when the grammar builds it.
$this->columns = null; $this->aggregate = null;
if (isset($results[0])) if (isset($results[0]))
{ {
return $results[0][$columns[0]]; return $results[0][$columns[0]];
...@@ -343,7 +366,7 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -343,7 +366,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
*/ */
public function delete($id = null) public function delete($id = null)
{ {
$query = $this->compileWheres($this); $query = $this->compileWheres();
$result = $this->collection->remove($query); $result = $this->collection->remove($query);
if (1 == (int) $result['ok']) if (1 == (int) $result['ok'])
......
<?php
require_once('vendor/autoload.php');
require_once('models/User.php');
use Jenssegers\Mongodb\Facades\DB;
class CacheTest extends PHPUnit_Framework_TestCase {
protected $app;
public function setUp()
{
include('tests/app.php');
$this->app = $app;
// test data
User::create(array('name' => 'John Doe', 'age' => 35, 'title' => 'admin'));
User::create(array('name' => 'Jane Doe', 'age' => 33, 'title' => 'admin'));
User::create(array('name' => 'Harry Hoe', 'age' => 13, 'title' => 'user'));
}
public function tearDown()
{
User::truncate();
}
public function testCache()
{
# auto generate cache key
$users = DB::collection('users')->where('age', '>', 10)->remember(10)->get();
$this->assertEquals(3, count($users));
$users = DB::collection('users')->where('age', '>', 10)->getCached();
$this->assertEquals(3, count($users));
# store under predefined cache key
$users = User::where('age', '>', 10)->remember(10, 'db.users')->get();
$this->assertEquals(3, count($users));
# get from cache driver
$cache = $this->app['cache'];
$users = $cache->get('db.users');
$this->assertEquals(3, count($users));
}
}
\ No newline at end of file
<?php <?php
require_once('vendor/autoload.php'); require_once('vendor/autoload.php');
require_once('models/User.php');
use Jenssegers\Mongodb\Facades\DB; use Jenssegers\Mongodb\Facades\DB;
......
...@@ -4,14 +4,21 @@ use Jenssegers\Mongodb\Model; ...@@ -4,14 +4,21 @@ use Jenssegers\Mongodb\Model;
use Jenssegers\Mongodb\DatabaseManager; use Jenssegers\Mongodb\DatabaseManager;
use Jenssegers\Mongodb\Facades\DB; use Jenssegers\Mongodb\Facades\DB;
use Illuminate\Events\Dispatcher; use Illuminate\Events\Dispatcher;
use Illuminate\Cache\ArrayStore;
use Illuminate\Cache\Repository;
# Fake app # Fake app
$app = array(); $app = array();
# Event dispatcher
$app['events'] = new Dispatcher; $app['events'] = new Dispatcher;
# Cache driver
$app['cache'] = new Repository(new ArrayStore);
# Database configuration # Database configuration
$app['config']['database.connections']['mongodb'] = array( $app['config']['database.connections']['mongodb'] = array(
'name' => 'mongodb',
'host' => 'localhost', 'host' => 'localhost',
'database' => 'unittest' 'database' => 'unittest'
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment