Created
August 30, 2015 09:10
-
-
Save rofr/d374189f7d4ff1cbd4e8 to your computer and use it in GitHub Desktop.
OrigoDB persistence plugin for XSockets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Found this on pastebin at http://pastebin.com/zkS4Wszb | |
// See comments below | |
/// <summary> | |
/// Key/Value pairs where key is string and value is object | |
/// </summary> | |
public interface IStorageModel | |
{ | |
object AddOrUpdate(string key, object entity); | |
bool Remove(string key); | |
void RemoveAll(); | |
IEnumerable<object> GetAll(); | |
object GetById(string key); | |
bool ContainsKey(string key); | |
} | |
[Serializable] | |
public class StorageModel : Model, IStorageModel | |
{ | |
private readonly object _locker = new object(); | |
private ConcurrentDictionary<string, object> Container { get; set; } | |
public StorageModel() | |
{ | |
this.Container = new ConcurrentDictionary<string, object>(); | |
} | |
public object AddOrUpdate(string key, object entity) | |
{ | |
lock (_locker) | |
{ | |
if (!Container.ContainsKey(key)) | |
Container.TryAdd(key, entity); | |
else | |
{ | |
Container[key] = entity; | |
} | |
} | |
return entity; | |
} | |
public bool Remove(string key) | |
{ | |
lock (_locker) | |
{ | |
object entity; | |
return Container.TryRemove(key, out entity); | |
} | |
} | |
public void RemoveAll() | |
{ | |
lock (_locker) | |
{ | |
Container = new ConcurrentDictionary<string, object>(); | |
} | |
} | |
public IEnumerable<object> GetAll() | |
{ | |
return Container.Values; | |
} | |
public object GetById(string key) | |
{ | |
return Container.ContainsKey(key) ? Container[key] : default(object); | |
} | |
public bool ContainsKey(string key) | |
{ | |
return Container.ContainsKey(key); | |
} | |
} | |
/// <summary> | |
/// The plugin that XSockets will pick up | |
/// </summary> | |
[Export(typeof(IPersistentStorageProvider))] | |
public interface IPersistentStorageProvider | |
{ | |
object AddOrUpdate(string key, object entity); | |
bool Remove(string key); | |
void RemoveAll(); | |
IEnumerable<object> GetAll(); | |
object GetById(string key); | |
bool ContainsKey(string key); | |
} | |
public class PersistentProvider : IPersistentStorageProvider | |
{ | |
private static StorageModel Provider { get; set; } | |
static PersistentProvider() | |
{ | |
Provider = Engine.For<StorageModel>(new EngineConfiguration(@"C:\temp\OrigoDB")).GetProxy(); | |
} | |
public object AddOrUpdate(string key, object entity) | |
{ | |
return Provider.AddOrUpdate(key, entity); | |
} | |
public bool Remove(string key) | |
{ | |
return Provider.Remove(key); | |
} | |
public void RemoveAll() | |
{ | |
Provider.RemoveAll(); | |
} | |
public IEnumerable<object> GetAll() | |
{ | |
return Provider.GetAll(); | |
} | |
public object GetById(string key) | |
{ | |
return Provider.GetById(key); | |
} | |
public bool ContainsKey(string key) | |
{ | |
return Provider.ContainsKey(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some issues with this code:
AddOrUpdate
andRemove
need to have[Command]
attributes, otherwise they will be interpreted as queries and calls will not be persisted!GetAll()
will potentially return a large amount of data, I would add some kind of paging or remove it.