Author: lindner
Date: Wed Mar 12 13:13:54 2008
New Revision: 636489

URL: http://svn.apache.org/viewvc?rev=636489&view=rev
Log:
Patch from Chris Chabot for SHINDIG-123

This patch adds a memcached backend, which is a bit slower in single server 
situations, but works very well with multi server setups.
Also renames FileCache.php to CacheFile.php for consitency.

Added:
    incubator/shindig/trunk/php/gadgets/src/CacheFile.php
      - copied, changed from r636486, 
incubator/shindig/trunk/php/gadgets/src/FileCache.php
    incubator/shindig/trunk/php/gadgets/src/CacheMemcache.php
Removed:
    incubator/shindig/trunk/php/gadgets/src/FileCache.php
Modified:
    incubator/shindig/trunk/php/gadgets/config.php

Modified: incubator/shindig/trunk/php/gadgets/config.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/config.php?rev=636489&r1=636488&r2=636489&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/config.php (original)
+++ incubator/shindig/trunk/php/gadgets/config.php Wed Mar 12 13:13:54 2008
@@ -25,7 +25,7 @@
        'remote_content' => 'BasicRemoteContent',
        'gadget_signer' => 'BasicGadgetSigner',
        'gadget_token' => 'BasicGadgetToken',
-       'data_cache' => 'FileCache',
+       'data_cache' => 'CacheFile',
 
        // gadget server specific settings
        'userpref_param_prefix'=> 'up_',
@@ -35,6 +35,11 @@
        
        // show debugging output ?
        'debug'=> true,
+
+       // if your using memcached, these values are used for locating the 
server
+       // if your not using memcached, ignore these values
+       'cache_host' => 'localhost',
+       'cache_port'   => 11211,
        
        // global cache age policy and location
        'cache_time'=> 24*60*60,

Copied: incubator/shindig/trunk/php/gadgets/src/CacheFile.php (from r636486, 
incubator/shindig/trunk/php/gadgets/src/FileCache.php)
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/src/CacheFile.php?p2=incubator/shindig/trunk/php/gadgets/src/CacheFile.php&p1=incubator/shindig/trunk/php/gadgets/src/FileCache.php&r1=636486&r2=636489&rev=636489&view=diff
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/FileCache.php (original)
+++ incubator/shindig/trunk/php/gadgets/src/CacheFile.php Wed Mar 12 13:13:54 
2008
@@ -31,7 +31,7 @@
 
 //TODO add cache stampeding prevention using file locking mechanisms
 
-class FileCache extends Cache {
+class CacheFile extends Cache {
        
        function get($key)
        {

Added: incubator/shindig/trunk/php/gadgets/src/CacheMemcache.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/gadgets/src/CacheMemcache.php?rev=636489&view=auto
==============================================================================
--- incubator/shindig/trunk/php/gadgets/src/CacheMemcache.php (added)
+++ incubator/shindig/trunk/php/gadgets/src/CacheMemcache.php Wed Mar 12 
13:13:54 2008
@@ -0,0 +1,98 @@
+<?
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ * 
+ */
+
+/*
+ * This class impliments memcached based caching It'll generally be more
+ * usefull in a multi-server envirionment then the file based caching,
+ * (in a single server setup file based caching is actually faster)
+ */
+
+class CacheMemcache extends Cache {
+       private $connection = false;
+
+       public function __construct()
+       {
+               global $config;
+               if (!function_exists('memcache_connect')) {
+                       throw new CacheException("Memcache functions not 
available");
+               }
+               if (empty($config['cache_host']) || 
empty($config['cache_port'])) {
+                       throw new CacheException("You need to configure a cache 
server host and port to use the memcache backend");
+               }
+               $this->host = $config['cache_host'];
+               $this->port = $config['cache_port'];
+       }
+
+       public function __destruct()
+       {
+               // if we were connected, close the connection again
+               if (is_resource($this->connection)) {
+                       memcache_close($this->connection);
+               }
+       }
+
+       // I prefer lazy initalization since the cache isn't used every request
+       // so this potentially saves a lot of overhead
+       private function connect()
+       {
+               if (!$this->connection = memcache_connect($this->host, 
$this->port)) {
+                       throw new CacheException("Couldn't connect to memcache 
server");
+               }
+       }
+       
+       private function check()
+       {
+               if (!$this->connection) {
+                       $this->connect();
+               }
+       }
+       
+       // using memcache_add behavior for cache stampeding prevention
+       private function add($key, $var, $timeout)
+       {
+               $this->check();
+               if (!memcache_add($this->connection, $key, $var, 0, $timeout)) {
+                       throw new CacheException("Couldn't add to cache");
+               }
+       }
+       
+       public function get($key)
+       {
+               $this->check();
+               if (($ret = memcache_get($this->connection, $key)) === false) {
+                       return false;
+               }
+               return $ret;
+       }
+       
+       public function set($key, $value)
+       {
+               global $config;
+               $this->check();
+               if (memcache_set($this->connection, $key, $value, 0, 
$config['cache_time']) === false) {
+                       throw new CacheException("Couldn't store data in 
cache");
+               }
+       }
+       
+       function delete($key)
+       {
+
+       }
+}


Reply via email to