Secondary key
We would like to use membase to build a cache of complicate price calculations. Our problem is that we need to clear the cache for single product when the user changes prices and not the entire cache.
For example:
Product 1 | Parameters | Price 1
Product 1 | Parameters | Price 2
Product 2 | Parameters | Price 1
When the user changes the system prices for product 1 we need to clear the cache but not for product 2.
We are currently doing this in MySQL but this is resulting in a huge number of inserts (the cache table usually contains 3-5 million records) and in our tests membase is significantly faster.
Is there any way of doing this with membase?
Thanks for the reply. This sounds very encouranging.
When is the v2.0 release expected?
Hi
I have have the same problem, I got around this by building the key for the product from other stored keys, below is some snippets from my memcached wrapper class
public function save($id, $data, $group = '', $ttl = null)
{
$ttl = ($ttl > 0)?time()+$ttl:0;
if ($this->_options['compression']){
$flag = MEMCACHE_COMPRESSED;
}
else{
$flag = 0;
}
$id = $this->_id($id,$group);
$return = $this->_memcache->set($id, array($data, time()), $flag, $ttl);
return $return;
}
public function valid($id, $group = ''){
$data = $this->get($id,$group);
if(is_array($data)){
return $data[1];
}
return false;
}
public function get($id, $group = ''){
$data = null;
$id = $this->_id($id,$group);
$data = $this->_memcache->get($id);
if (is_array($data)){
return $data[0];
}
return null;
}
public function destroy($id, $group = '')
{
$id = $this->_id($id,$group);
return $this->_memcache->delete($id,0);
}
public function flush($group = ''){
if(!empty($group)){
$groupName = $this->_options['prefix'].'-'.$group;
return $this->destroy('internal-group-'.$groupName);
}
return $this->_memcache->flush();
}
private function _id($key, $group = ''){
if(!empty($group)){
$groupName = $this->_options['prefix'].'-'.$group;
$groupKey = $this->get('internal-group-'.$groupName);
if(empty($groupKey)){
$groupKey = substr($this->hash(microtime()),0,16);
$this->save('internal-group-'.$groupName,$groupKey,'',(3600*24*7));
}
$returnKey = $groupKey.'::'.$key;
}
else{
$returnKey = $key;
}
if(strlen($returnKey) > 250){ //quick hack to sort key lenght limit
$returnKey = $this->hash($returnKey);
}
return $returnKey;
}
protected function hash($str)
{
return md5($str).':'.md5(strrev($str));
}this might give some help on how to solve your problem.
Steven
Hi there, thanks for your inquiry.
You could accomplish this in a number of ways:
-Use our TAP interface to stream out all the data from Membase and issue deletes for the keys that match a given product. The added advantage here is that you can connect to a TAP stream and be notified when a given key is changed in order to trigger a delete if you like.
-Setup separate buckets for each product so that you can manage each individually. I doubt this will actually be useful to you if you have millions of products, but thought it was worth pointing out.
-Our 2.0 release will include querying and indexing via CouchDB so that you can "search" for keys matching a given product and issue deletes for them.
How does that all sound?
Perry
Forum support is great for free but sometimes you need a guaranteed response time and dedicated resources for your questions or issues.
Consider purchasing enterprise-level support from Couchbase: http://www.couchbase.com/products-and-services/overview
Call or email "sales -at- couchbase-dot- com" today!