Commit e2e6bbb2 authored by Evan Prodromou's avatar Evan Prodromou

caching layer for DB/DataObject

I added a new class, Memcached_DataObject, that will (optionally)
fetch data out of a memcached server if it's available. This only
works on 'staticGet'.

Methods that write to the database (insert, update, delete) will clear
and set the cache correctly, too.

darcs-hash:20080926160941-5ed1f-922de078b4c1941853ad014edf9a17fae486f8cf.gz
parent 7ff02bb7
......@@ -2,9 +2,9 @@
/**
* Table Definition for avatar
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Avatar extends DB_DataObject
class Avatar extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -21,7 +21,7 @@ class Avatar extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Avatar',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Avatar',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for confirm_address
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Confirm_address extends DB_DataObject
class Confirm_address extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -20,7 +20,7 @@ class Confirm_address extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Confirm_address',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Confirm_address',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for consumer
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Consumer extends DB_DataObject
class Consumer extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -16,7 +16,7 @@ class Consumer extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Consumer',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Consumer',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for fave
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Fave extends DB_DataObject
class Fave extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -15,7 +15,7 @@ class Fave extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Fave',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Fave',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for foreign_link
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Foreign_link extends DB_DataObject
class Foreign_link extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -21,7 +21,7 @@ class Foreign_link extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Foreign_link',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Foreign_link',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for foreign_service
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Foreign_service extends DB_DataObject
class Foreign_service extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -17,7 +17,7 @@ class Foreign_service extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Foreign_service',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Foreign_service',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for foreign_subscription
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Foreign_subscription extends DB_DataObject
class Foreign_subscription extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -16,7 +16,7 @@ class Foreign_subscription extends DB_DataObject
public $created; // datetime() not_null
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Foreign_subscription',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Foreign_subscription',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for foreign_user
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Foreign_user extends DB_DataObject
class Foreign_user extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -18,7 +18,7 @@ class Foreign_user extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Foreign_user',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Foreign_user',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for invitation
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Invitation extends DB_DataObject
class Invitation extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -17,7 +17,7 @@ class Invitation extends DB_DataObject
public $created; // datetime() not_null
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Invitation',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Invitation',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
<?php
/*
* Laconica - a distributed open-source microblogging tool
* Copyright (C) 2008, Controlez-Vous, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('LACONICA')) { exit(1); }
require_once 'classes/Memcached_DataObject.php';
class Memcached_DataObject extends DB_DataObject
{
static function &staticGet($cls, $k, $v=NULL) {
$i = $this->getcached($cls, $k, $v);
if (!is_null($i)) {
return $i;
} else {
$i = parent::staticGet($k, $v);
if (!is_null($i)) {
$i->encache();
}
return $i;
}
}
function insert() {
$result = parent::insert();
if ($result) {
$this->encache();
}
return $result;
}
function update($orig=NULL) {
if (!is_null($orig)) {
$orig->decache(); # might be different keys
}
$result = parent::update($orig);
if ($result) {
$this->encache();
}
}
function delete() {
$this->decache(); # while we still have the values!
return parent::delete();
}
static function memcache() {
if (!common_config('memcached', 'enabled')) {
return NULL;
} else {
$cache = new Memcache();
$res = $cache->connect(common_config('memcached', 'server'),
common_config('memcached', 'port'));
return ($res) ? $cache : NULL;
}
}
static function cacheKey($cls, $k, $v) {
return common_cache_key(strtolower($cls) . ':' .
$k . ':' .
$v);
}
static function getcached($cls, $k, $v) {
$c = Memcached_DataObject::memcache();
if (!$c) {
return false;
} else {
return $c->get(Memcached_DataObject::cacheKey($cls, $k, $v));
}
}
function keyTypes() {
global $_DB_DATAOBJECT;
if (!isset($_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"])) {
$this->databaseStructure();
}
return $_DB_DATAOBJECT['INI'][$this->_database][$this->__table."__keys"];
}
function encache() {
$c = $this->memcache();
if (!$c) {
return false;
} else {
$primary = array();
$types = ksort($this->keyTypes());
foreach ($types as $key => $type) {
if ($type == 'K') {
$primary[] = $key;
} else {
$c->set($this->cacheKey($this->tableName(), $key, $this->$key),
$this);
}
}
# XXX: figure out what to do with compound pkeys
if (count($primary) == 1) {
$key = $primary[0];
$c->set($this->cacheKey($this->tableName(), $key, $this->$key),
$this);
}
}
}
function decache() {
$c = $this->memcache();
if (!$c) {
return false;
} else {
$primary = array();
$types = ksort($this->keyTypes());
foreach ($types as $key => $type) {
if ($type == 'K') {
$primary[] = $this->$key;
} else {
$c->delete($this->cacheKey($this->tableName(), $key, $this->$key),
$this);
}
}
# XXX: figure out what to do with compound pkeys
if (count($primary) == 1) {
$key = $primary[0];
$c->delete($this->cacheKey($this->tableName(), $key, $this->$key),
$this);
}
}
}
}
......@@ -2,9 +2,9 @@
/**
* Table Definition for message
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Message extends DB_DataObject
class Message extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -22,7 +22,7 @@ class Message extends DB_DataObject
public $source; // varchar(32)
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Message',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Message',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for nonce
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Nonce extends DB_DataObject
class Nonce extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -18,7 +18,7 @@ class Nonce extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Nonce',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Nonce',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -22,9 +22,9 @@ if (!defined('LACONICA')) { exit(1); }
/**
* Table Definition for notice
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Notice extends DB_DataObject
class Notice extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -43,7 +43,7 @@ class Notice extends DB_DataObject
public $source; // varchar(32)
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Notice',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Notice',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for notice_source
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Notice_source extends DB_DataObject
class Notice_source extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -17,7 +17,7 @@ class Notice_source extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Notice_source',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Notice_source',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for notice_tag
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Notice_tag extends DB_DataObject
class Notice_tag extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -15,7 +15,7 @@ class Notice_tag extends DB_DataObject
public $created; // datetime() not_null
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Notice_tag',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Notice_tag',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -22,9 +22,9 @@ if (!defined('LACONICA')) { exit(1); }
/**
* Table Definition for profile
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Profile extends DB_DataObject
class Profile extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -41,7 +41,7 @@ class Profile extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Profile',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Profile',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for queue_item
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Queue_item extends DB_DataObject
class Queue_item extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -16,7 +16,7 @@ class Queue_item extends DB_DataObject
public $claimed; // datetime()
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Queue_item',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Queue_item',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for remember_me
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Remember_me extends DB_DataObject
class Remember_me extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -15,7 +15,7 @@ class Remember_me extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Remember_me',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Remember_me',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -22,9 +22,9 @@ if (!defined('LACONICA')) { exit(1); }
/**
* Table Definition for remote_profile
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Remote_profile extends DB_DataObject
class Remote_profile extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -38,7 +38,7 @@ class Remote_profile extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Remote_profile',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Remote_profile',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for reply
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Reply extends DB_DataObject
class Reply extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -16,7 +16,7 @@ class Reply extends DB_DataObject
public $replied_id; // int(4)
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Reply',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Reply',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for sms_carrier
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Sms_carrier extends DB_DataObject
class Sms_carrier extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -17,7 +17,7 @@ class Sms_carrier extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Sms_carrier',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Sms_carrier',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -22,9 +22,9 @@ if (!defined('LACONICA')) { exit(1); }
/**
* Table Definition for subscription
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Subscription extends DB_DataObject
class Subscription extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -38,7 +38,7 @@ class Subscription extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Subscription',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Subscription',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for token
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class Token extends DB_DataObject
class Token extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -19,7 +19,7 @@ class Token extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Token',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Token',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
......@@ -27,11 +27,11 @@ define('WITHFRIENDS_CACHE_WINDOW', 61);
/**
* Table Definition for user
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
require_once 'Validate.php';
require_once(INSTALLDIR.'/lib/noticewrapper.php');
class User extends DB_DataObject
class User extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -65,7 +65,7 @@ class User extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('User',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......@@ -109,7 +109,12 @@ class User extends DB_DataObject
}
$qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
' WHERE id = ' . $this->id;
return $this->query($qry);
$orig->decache();
$result = $this->query($qry);
if ($result) {
$this->encache();
}
return $result;
}
function allowed_nickname($nickname) {
......
......@@ -2,9 +2,9 @@
/**
* Table Definition for user_openid
*/
require_once 'DB/DataObject.php';
require_once 'classes/Memcached_DataObject.php';
class User_openid extends DB_DataObject
class User_openid extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
......@@ -17,7 +17,7 @@ class User_openid extends DB_DataObject
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('User_openid',$k,$v); }
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User_openid',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
......
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