Collection of simple pieces of utility code
Simple lightweight object in-memory cache, with a background timer to remove expired objects.
Fast in-memory cache for data that are expensive to create and can be used in a thread-safe manner.
All stored items are kept in concurrent data structures (ConcurrentDictionary) to allow multi-thread usage of the MemoryCache static methods. Note that the stored objects must be thread-safe, since the same instance of an object can and will be returned by multiple calls of Get methods. If you wish to use non-thread safe object instances you must use the Remove method to atomically (safelly) get and remove the object instance from the cache.
Note: this nuget package contains csharp source code and depends on System.Collections.Concurrent introduced in .Net 4.0.
Configuration
Simple configuration of the MemoryCache settings. It should be done for each type, since since the JIT compiler will generate different code at run time, MemoryCache is considered a different class, for example, from MemoryCache or MemoryCache<byte[]>.
// setup (called once in application initialization)// set a duration of an item in the cache to 1 secondMemoryCache<string>.Expiration=TimeSpan.FromSeconds(1);// set the internal maintenance timed task to run each 500 ms removing expired itemsMemoryCache<string>.MaintenanceStep=TimeSpan.FromMilliseconds(500);// Our event, if we want to treat the removed expired itemsMemoryCache<string>.OnExpiration+=(stringkey,stringitem)=>{// do something};Example
// store an itemMemoryCache<string>.Set("k1","test");// get itstringvalue=MemoryCache<string>.Get("k1");// check if we got itif(value!=null){// ok we got it!}Using a factory to create a new item if the in memory cache does not have it.
// prepare our factoryvarfactory=(stringkey)=>{returnkey+DateTime.Now.ToString();};// try to get the associated value if the cache does not have it// then use the factory to create a new one, store and return it.stringvalue=MemoryCache<string>.GetOrAdd("key2",factory);Using a more complex concurrent scenario where, if the item is not found, we only want to call the factory only once. So all concurrent calls will wait until factory (that is guaranteed to be called only once) has created and stored the new item, and will return it.
// prepare our factoryvarfactory=(stringkey)=>{returnkey+DateTime.Now.ToString();};// try to get the associated value if the cache does not have it// then use the factory to create a new one, store and return it.// It the factory takes more than 250 milliseconds to create then new instance,// it will exit returning the default value for the classstringvalue=MemoryCache<string>.GetOrSyncAdd("key2",factory,TimeSpan.FromMilliseconds(250));// check if we got itif(value!=null){// ok we got it!}Synchronization helper: a static lock collection associated with a key.
NamedLock manages the lifetime of critical sections that can be accessed by a key (name) throughout the application. It also have some helper methods to allow a maximum wait time (timeout) to acquire the lock and safely release it.
Note: this nuget package contains c# source code and depends on System.Collections.Concurrent introduced in .Net 4.0.
Example
Simple usage where we try to acquire the lock for 100 ms. So if somewhere else in our application this same lock was already acquired, we will wait until we acquire the lock or 100 ms has passed.
stringkey="our lock name";using(varpadlock=newNamedLock(key)){if(padlock.Enter(TimeSpan.FromMilliseconds(100))){// do something as we now own the lock}else{// do some other thing since we could not aquire the lock}}Another usage example with a static helper method.
stringkey="our lock name";using(varpadlock=NamedLock.CreateAndEnter(key,TimeSpan.FromMilliseconds(100))){if(padlock.IsLocked){// do something }}A simple lightweight object pool for fast and simple object reuse.
Fast lightweight thread-safe object pool for objects that are expensive to create or could efficiently be reused.
Note: this nuget package contains c# source code and depends on System.Collections.Concurrent introduced in .Net 4.0.
Example
// Get or create a new random generatorRandomrnd=ObjectPool<Random>.Get(CreateRandomGenerator);// start generating random numberstry{// ...varnum=rnd.Next(max);// ...}finally{// Release the random generator by putting it back in the object poolObjectPool<Random>.Put(rnd);}// our factoryprivatestaticRandomCreateRandomGenerator(){returnnewRandom((int)DateTime.UtcNow.Ticks);}Useful for:
- processing items in batches;
- grouping data for later processing;
- async processing (consumer/producer);
- etc.
Note: this nuget package contains c# source code and depends on System.Collections.Concurrent introduced in .Net 4.0.
Configuration
Simple configuration of the TimedQueue settings. It should be done for each type, since the JIT compiler will generate differente code at run time, TimedQueue<string> is considered a different class, for example, from TimedQueue<StringBuilder> or TimedQueue<byte[]>.
// setup (called once in application initialization)// set the queue timed task to run each 500 ms executing the registered actionSimpleHelpers.TimedQueue<Our_Object>.TimerStep=TimeSpan.FromMilliseconds(500);// Our event, if we want to treat the removed expired itemsSimpleHelpers.TimedQueue<Our_Object>.OnExecution+=(IEnumerable<Our_Object>items)=>{foreach(varevtinitems)// do something};Example
// our method that does an network call to store out object // and keeps retrying in case of failurepublicstaticvoidSaveEvent(Our_Objectevt){// try to savetry{SaveOverTheInternet(evt);}catch(System.IO.IOExceptionex){// log.Error (ex);SimpleHelpers.TimedQueue<Our_Object>.Put(evt);}}Simple configuration manager to get and set the values in the AppSettings section of the default configuration file.
Note: this nuget package contains csharp source code and depends on Generics introduced in .Net 2.0.
Configuration
// setup (called once in application initialization)// set to add any new keys added during the application executionConfigManager.AddNonExistingKeys=true;** Example **
stringaddress=ConfigManager.Get("MongoDBaddress","localhost");intport=ConfigManager.Get("MongoDBport",21766);Simple key value storage using sqlite.
All member methods are thread-safe, so a instance can be safely be accessed by multiple threads.
All stored items are serialized to json by json.net.
Note: this nuget package contains csharp source code and depends on .Net 4.0.
Configuration
// setup:SQLiteStorage<My_Class>db=newSQLiteStorage<My_Class>("path_to_my_file.sqlite",SQLiteStorageOptions.UniqueKeys());** Example **
// create a new instanceSQLiteStorage<My_Class>db=newSQLiteStorage<My_Class>("path_to_my_file.sqlite",SQLiteStorageOptions.UniqueKeys());// save an item with a key associateddb.Set("my_key_for_this_item",newMy_Class());// get it backMy_Classmy_obj=db.Get("my_key_for_this_item").FirstOrDefault();// to save any changes, just call set againdb.Set("my_key_for_this_item",my_obj);// get all stored itemsforeach(varitemindb.Get()){// ...}