uw Sat Mar 3 11:14:36 2001 EDT
Modified files:
/php4/pear/Cache Container.php
Log:
PR:
What does PR mean - Public Relations? Anyway: new cache structure/features
- allowed to group cache datasets in a group
- allowed to add userdata [CHAR(255)] to a dataset in the cache
More explanations will follow on the PEAR list.
Submitted by:
Ulf Wendel <[EMAIL PROTECTED]>
Reviewed by:
myself ;-)
works on my development system and my homepage
Index: php4/pear/Cache/Container.php
diff -u php4/pear/Cache/Container.php:1.3 php4/pear/Cache/Container.php:1.4
--- php4/pear/Cache/Container.php:1.3 Fri Mar 2 06:30:03 2001
+++ php4/pear/Cache/Container.php Sat Mar 3 11:14:36 2001
@@ -16,7 +16,7 @@
// | Sebastian Bergmann <[EMAIL PROTECTED]> |
// +----------------------------------------------------------------------+
//
-// $Id: Container.php,v 1.3 2001/03/02 14:30:03 uw Exp $
+// $Id: Container.php,v 1.4 2001/03/03 19:14:36 uw Exp $
/**
* Common base class of all cache storage container.
@@ -36,7 +36,7 @@
* not recommended!
*
* @author Ulf Wendel <[EMAIL PROTECTED]>
-* @version $Id: Container.php,v 1.3 2001/03/02 14:30:03 uw Exp $
+* @version $Id: Container.php,v 1.4 2001/03/03 19:14:36 uw Exp $
* @package Cache
* @access public
* @abstract
@@ -59,6 +59,15 @@
*/
var $id = "";
+
+ /**
+ * Cache group of a preloaded dataset
+ *
+ * @var string
+ */
+ var $group = "";
+
+
/**
* Expiration timestamp of a preloaded dataset.
*
@@ -66,66 +75,108 @@
*/
var $expires = 0;
+
/**
* Value of a preloaded dataset.
*
* @var string
*/
- var $data = "";
+ var $cachedata = "";
+
/**
+ * Preloaded userdata field.
+ *
+ * @var string
+ */
+ var $userdata = "";
+
+
+ /**
* Flag indicating that the dataset requested for preloading is unknown.
*
* @var boolean
*/
var $unknown = true;
+
/**
* Encoding mode for cache data: base64 or addslashes() (slash).
*
* @var string base64 or slash
*/
var $encoding_mode = "base64";
+
/**
* Loads a dataset from the cache.
*
* @param string dataset ID
+ * @param string cache group
* @return mixed dataset value or NULL on failure
* @access public
*/
- function load($id) {
+ function load($id, $group) {
if ($this->preload) {
- if ($this->id != $id)
- $this->preload($id);
+ if ($this->id != $id || $this->group != $group)
+ $this->preload($id, $group);
- return $this->data;
+ return $this->cachedata;
} else {
- list( , $data) = $this->fetch($id);
+ list( , $data, ) = $this->fetch($id, $group);
return $data;
}
} // end func load
+
+ /**
+ * Returns the userdata field of a cached data set.
+ *
+ * @param string dataset ID
+ * @param string cache group
+ * @return string userdata
+ * @access public
+ */
+ function getUserdata($id, $group) {
+
+ if ($this->preload) {
+
+ if ($this->id != $id || $this->group != $group)
+ $this->preload($id, $group);
+
+ return $this->userdata;
+
+ } else {
+
+ list( , , $userdata) = $this->fetch($id, $group);
+ return $userdata;
+
+ }
+
+ } // end func getUserdata
+
+
/**
* Checks if a dataset is expired.
*
* @param string dataset ID
+ * @param string cache group
* @param integer maximum age timestamp
* @return boolean
* @access public
*/
- function isExpired($id, $max_age = 0) {
+ function isExpired($id, $group, $max_age) {
if ($this->preload) {
- if ($this->id != $id)
- $this->preload($id);
+ if ($this->id != $id || $this->group != $group)
+ $this->preload($id, $group);
if ($this->unknown)
return false;
@@ -133,11 +184,11 @@
} else {
// check if at all it is cached
- if (!$this->isCached($id))
+ if (!$this->isCached($id, $group))
return false;
// I'm lazy...
- list($this->expires, ) = $this->fetch($id);
+ list($this->expires, , ) = $this->fetch($id, $group);
}
// endless
@@ -147,7 +198,7 @@
// you feel fine, Ulf?
if ($expired = ($this->expires <= time() || ($max_age && ($this->expires <=
$max_age))) ) {
- $this->delete($id);
+ $this->delete($id, $group);
$this->flushPreload();
}
@@ -155,24 +206,26 @@
return $expired;
} // end func isExpired
+
/**
* Checks if a dataset is cached.
*
* @param string dataset ID
+ * @param string cache group
* @return boolean
*/
- function isCached($id) {
+ function isCached($id, $group) {
if ($this->preload) {
- if ($this->id != $id)
- $this->preload($id);
+ if ($this->id != $id || $this->group != $group)
+ $this->preload($id, $group);
return !($this->unknown);
} else {
- return $this->idExists($id);
+ return $this->idExists($id, $group);
}
@@ -186,12 +239,13 @@
* Fetches a dataset from the storage medium.
*
* @param string dataset ID
- * @return array format: [expire date, cached data]
+ * @param string cache group
+ * @return array format: [expire date, cached data, user data]
* @throws CacheError
* @abstract
*/
- function fetch($id) {
- return array(NULL, NULL);
+ function fetch($id, $group) {
+ return array(NULL, NULL, NULL);
} // end func fetch
/**
@@ -200,55 +254,65 @@
* @param string dataset ID
* @param mixed data to store
* @param mixed userdefined expire date
+ * @param string cache group
+ * @param string additional userdefined data
* @return boolean
* @throws CacheError
* @access public
* @abstract
*/
- function save($id, $data, $expire = 0) {
- $this->flushPreload($id);
+ function save($id, $data, $expire, $group, $userdata) {
+
+ // QUESTION: Should we update the preload buffer instead?
+ // Don't think so as the sequence save()/load() is unlikely.
+ $this->flushPreload($id, $group);
+
return NULL;
} // end func save
-
/**
* Deletes a dataset.
*
* @param string dataset ID
+ * @param string cache group
* @return boolean
* @access public
* @abstract
*/
- function delete($id) {
- $this->flushPreload($id);
+ function delete($id, $group) {
+ $this->flushPreload($id, $group);
return NULL;
} // end func delete
+
/**
- * Flushes the cache - removes all caches datasets from the cache
+ * Flushes the cache - removes all caches datasets from the cache.
*
- * @return integer Number of removed datasets, -1 on failure
- * @throws CacheError
+ * @param string If a cache group is given only the group will be flushed
+ * @return integer Number of removed datasets, -1 on failure
* @access public
* @abstract
*/
- function flush() {
+ function flush($group) {
$this->flushPreload();
return NULL;
} // end func flush
+
/**
- * Checks if a dataset exists
+ * Checks if a dataset exists.
*
* @param string dataset ID
+ * @param string cache group
* @return boolean
* @access public
* @abstract
*/
- function idExists($id) {
+ function idExists($id, $group) {
return NULL;
} // end func idExists
+
/**
* Starts the garbage collection.
*
@@ -259,28 +323,27 @@
$this->flushPreload();
} // end func garbageCollection
+
/**
* Does a speculative preload of a dataset
*
* @param string dataset ID
+ * @param string cache group
* @return boolean
*/
- function preload($id) {
+ function preload($id, $group) {
// whatever happens, remember the preloaded ID
- $this->id = $id;
+ $this->id = $id;
+ $this->group = $group;
- list($this->expires, $this->data) = $this->fetch($id);
+ list($this->expires, $this->cachedata, $this->userdata) = $this->fetch($id,
+$group);
- if (NULL === $this->data) {
-
+ if (NULL === $this->expires) {
+
// Uuups, unknown ID
+ $this->flushPreload();
- // clear the internal preload values
- $this->data = "";
- $this->expires = -1;
- $this->unknown = true;
-
return false;
}
@@ -289,6 +352,7 @@
return true;
} // end func preload
+
/**
* Flushes the internal preload buffer.
*
@@ -296,20 +360,27 @@
* to preevent differences between the preloaded values and
* the real cache contents.
*
+ * @param string dataset ID, if left out the preloaded values will be flushed.
+ * If given the preloaded values will only be flushed if they are
+ * equal to the given id and group
+ * @param string cache group
* @see preload()
*/
- function flushPreload($id = "") {
+ function flushPreload($id = "", $group = "default") {
- if (!$id || $this->id == $id) {
+ if (!$id || ($this->id == $id && $this->group == $group)) {
// clear the internal preload values
$this->id = "";
- $this->data = "";
+ $this->group = "";
+ $this->cachedata = "";
+ $this->userdata = "";
$this->expires = -1;
$this->unknown = true;
}
} // end func flushPreload
+
/**
* Imports the requested datafields as object variables if allowed
*
@@ -324,8 +395,9 @@
} // end func setOptions
+
/**
- * Encodes the data for the storage container
+ * Encodes the data for the storage container.
*
* @var mixed data to encode
*/
@@ -335,8 +407,10 @@
return base64_encode(serialize($data));
else
return addslashes(serialize($data));
+
} // end func encode
+
/**
* Decodes the data from the storage container.
*
@@ -352,4 +426,4 @@
} // end func decode
}
-?>
+?>
\ No newline at end of file
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]