Reputation: 637
I am using Couchbase 3.o with .Net SDK (V2.0). I am using it as distributed cache. I need to implement "Clear" method to clear all items in cache. That translates to deleting all documents in bucket. I do not see any method on Bucket or Cluster to do so. Is there some other API that I can use?
Thanks
Upvotes: 4
Views: 5821
Reputation: 464
The Couchbase .NET SDK has a flush command, here is an example of how to use it:
var configuration = new ClientConfiguration
{
Servers = new List<Uri>
{
new Uri(ConfigurationManager.AppSettings["bootstrapUrl"])
}
};
using (var cluster = new Cluster(configuration))
{
using (var bucket = cluster.OpenBucket())
{
var manager = bucket.CreateManager("Administrator", "");
var result = manager.Flush();
Assert.IsTrue(result.Success);
}
}
Upvotes: 4
Reputation: 4845
The keyword you are looking for is "flush". It is for just such a task. I know there is a REST API call to do this on the server, but I think there is a way in the .NET SDK 2.0 to do this as well. Just make sure you turn on the flush capability at the bucket level on the server. Otherwise it will never work.
As an overall cache invalidation strategy, I'd rather see you set a TTL on each piece of data you put into Couchbase and let Couchbase delete those objects over time instead of the overhead of dumping the cache the way you are talking about.
On a completely different note, make sure you are using the GA version of the 2.0 .NET SDK as it just came out the other day and Couchbase 3.0.1 as there was a nasty bug in the 3.0 release.
Upvotes: 2
Reputation: 28301
This is not a standard operation in couchbase (nor in memcached). The best way to do that is to implement a view in couchbase server that will list all document keys, then call Remove on all of the keys.
The view definition would be like this (in javascript):
function (doc, meta)
{
emit(meta.id, null);
}
Upvotes: 1