Skip to content

Instantly share code, notes, and snippets.

@diegocaxito
Last active December 17, 2015 09:39
Show Gist options
  • Save diegocaxito/5589074 to your computer and use it in GitHub Desktop.
Save diegocaxito/5589074 to your computer and use it in GitHub Desktop.
A simple learning test with Couchbase as .Net memory cache strategy.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="couchbase" type="Couchbase.Configuration.CouchbaseClientSection, Couchbase"/>
</configSections>
<couchbase>
<servers bucket="default" bucketPassword="">
<add uri="http://diegocaxito.couchbase.com:8091/pools/default"/>
</servers>
</couchbase>
</configuration>
using Couchbase;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System;
namespace Poc.CouchBase.Test
{
[TestClass]
public class CouchbaseLearningTests
{
[TestInitialize]
public void InitializeUnit()
{
CouchbaseManager.Instance.FlushAll();
}
[TestMethod]
public void When_add_new_item_should_store_item()
{
var stored = CouchbaseManager.StoreInMemory(keyToStore, BuildObjectToStore());
Assert.IsTrue(stored);
}
[TestMethod]
public void When_add_new_item_and_get_item_should_get_not_null_object()
{
StoreDefaultObjectInMemory();
ShouldReturnObjectData();
}
[TestMethod]
public void When_add_new_item_and_get_item_should_get_item_with_same_content()
{
StoreDefaultObjectInMemory();
var storedObject = CouchbaseManager.GetInMemory<MyData>(keyToStore);
Assert.AreEqual(BuildObjectToStore(), storedObject);
}
[TestMethod]
public void When_add_new_item_and_flush_memory_then_should_return_null() {
StoreDefaultObjectInMemory();
CouchbaseManager.Instance.FlushAll();
ShouldNotReturnObjectData();
}
[TestMethod]
public void When_add_new_item_to_expires_in_future_and_get_data_to_key_then_should_return_data() {
CouchbaseManager.StoreInMemory(keyToStore, BuildObjectToStore(), minutesToAdd);
ShouldReturnObjectData();
}
[TestMethod]
public void When_add_new_item_to_expires_in_past_and_get_data_to_key_then_should_not_return_data()
{
CouchbaseManager.StoreInMemory(keyToStore, BuildObjectToStore(), -minutesToAdd);
ShouldNotReturnObjectData();
}
private const string keyToStore = "my_key";
private const int minutesToAdd = 1;
private static void StoreDefaultObjectInMemory()
{
CouchbaseManager.StoreInMemory(keyToStore, BuildObjectToStore());
}
private static void ShouldNotReturnObjectData()
{
var storedObject = CouchbaseManager.GetInMemory<MyData>(keyToStore);
Assert.IsNull(storedObject);
}
private static void ShouldReturnObjectData()
{
var objectStored = CouchbaseManager.Instance.Get(keyToStore);
Assert.IsNotNull(objectStored);
}
private static MyData BuildObjectToStore()
{
return new MyData { Content = "my content" };
}
}
public class MyData
{
public string Content { get; set; }
public override bool Equals(object obj)
{
return obj != null && this.Content == ((MyData)obj).Content;
}
}
public static class CouchbaseManager
{
private static readonly CouchbaseClient client;
static CouchbaseManager()
{
client = new CouchbaseClient();
}
public static CouchbaseClient Instance { get { return client; } }
public static bool StoreInMemory<T>(string myKey, T dataObjectToStore) where T : class
{
return CouchbaseManager.Instance.Store(Enyim.Caching.Memcached.StoreMode.Set, myKey, JsonConvert.SerializeObject(dataObjectToStore));
}
public static bool StoreInMemory<T>(string keyToStore, T dataObjectToStore, int minutesToAdd) where T : class
{
return CouchbaseManager.Instance.Store(Enyim.Caching.Memcached.StoreMode.Set, keyToStore, JsonConvert.SerializeObject(dataObjectToStore), DateTime.Now.AddMinutes(minutesToAdd));
}
public static T GetInMemory<T>(string myKey) where T : class
{
var objectStored = CouchbaseManager.Instance.Get(myKey);
return ExistsData(objectStored) ? JsonConvert.DeserializeObject<T>(objectStored.ToString()) : default(T);
}
private static bool ExistsData(object objectStored)
{
return objectStored != null && !string.IsNullOrWhiteSpace(objectStored.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment