Преглед изворни кода

Common:

27.12.2019 v.1.0.0.6
New
 - Add Singleton implementation
 - Add WeakReference generic pattern
 - Add VolatileCache implementation
Dalibor Votruba пре 6 година
родитељ
комит
8f0fc972b7

+ 38 - 0
Common/Cache/Cache.cs

@@ -0,0 +1,38 @@
+namespace Quadarax.Foundation.Common.Cache
+{
+    public abstract class Cache<TKey, TValue>
+    {
+        public bool Enabled { get; set; }
+
+        public TValue this[TKey key]
+        {
+            get
+            {
+                if (!this.Enabled)
+                    return default(TValue);
+                return this.GetValue(key);
+            }
+            set
+            {
+                if (!this.Enabled)
+                    return;
+                this.SetValue(key, value);
+            }
+        }
+
+        public bool Remove(TKey key)
+        {
+            if (!this.Enabled)
+                return false;
+            return this.RemoveByKey(key);
+        }
+
+        public abstract bool Invalidate(TKey key);
+
+        protected abstract TValue GetValue(TKey key);
+
+        protected abstract void SetValue(TKey key, TValue value);
+
+        protected abstract bool RemoveByKey(TKey key);
+    }
+}

+ 134 - 0
Common/Cache/VolatileCache.cs

@@ -0,0 +1,134 @@
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+
+namespace Quadarax.Foundation.Common.Cache
+{
+    public class VolatileCache<TKey, TValue> : Cache<TKey, TValue>
+    {
+        private ConcurrentDictionary<TKey, TValue> cache = new ConcurrentDictionary<TKey, TValue>();
+
+        #region *** Properties ***
+
+        public int Count
+        {
+            get
+            {
+                return this.cache.Count;
+            }
+        }
+
+        public TKey[] Keys
+        {
+            get
+            {
+                ICollection<TKey> keys = this.cache.Keys;
+                TKey[] array = new TKey[keys.Count];
+                keys.CopyTo(array, 0);
+                return array;
+            }
+        }
+
+        public TValue[] Values
+        {
+            get
+            {
+                ICollection<TValue> values = this.cache.Values;
+                TValue[] array = new TValue[values.Count];
+                values.CopyTo(array, 0);
+                return array;
+            }
+        }
+
+        #endregion
+
+        #region *** Constructors ***
+
+        public VolatileCache()
+        {
+            this.Enabled = true;
+        }
+
+        #endregion
+        
+        #region *** Overrides ***
+
+        protected override TValue GetValue(TKey key)
+        {
+            TValue v;
+            if (this.cache.TryGetValue(key, out v))
+                return v;
+            return default(TValue);
+        }
+
+        protected override void SetValue(TKey key, TValue value)
+        {
+            this.cache[key] = value;
+        }
+
+        protected override bool RemoveByKey(TKey key)
+        {
+            TValue v;
+            return this.cache.TryRemove(key, out v);
+        }
+
+        public override bool Invalidate(TKey key)
+        {
+            return this.Remove(key);
+        }
+
+        #endregion
+
+        public bool ContainsKey(TKey key)
+        {
+            return this.cache.ContainsKey(key);
+        }
+
+        public bool FindAndAddValue(TKey key, TValue value)
+        {
+            return this.cache.TryAdd(key, value);
+        }
+
+        public bool Remove(TKey key, out TValue value)
+        {
+            value = default(TValue);
+            if (!this.Enabled)
+                return false;
+            return this.cache.TryRemove(key, out value);
+        }
+
+        public void Clear()
+        {
+            this.cache = new ConcurrentDictionary<TKey, TValue>();
+        }
+
+        public bool TryGetValue(TKey key, out TValue retval)
+        {
+            return this.cache.TryGetValue(key, out retval);
+        }
+
+        public bool TryGetValueOrFetchDummyValue(TKey key, Func<TValue> fetchDummyValueDelegate, out TValue retval)
+        {
+            bool flag = this.TryGetValue(key, out retval);
+            if (!flag && fetchDummyValueDelegate != null)
+            {
+                flag = this.cache.TryGetValue(key, out retval);
+                if (!flag)
+                {
+                    retval = fetchDummyValueDelegate();
+                    this.cache[key] = retval;
+                }
+            }
+            return flag;
+        }
+
+        public TValue[] GetAllValuesAndClear()
+        {
+            ICollection<TValue> values = this.cache.Values;
+            TValue[] array = new TValue[values.Count];
+            values.CopyTo(array, 0);
+            this.Clear();
+            return array;
+        }
+    }
+}

+ 4 - 0
Common/Common.csproj

@@ -50,8 +50,12 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Cache\Cache.cs" />
+    <Compile Include="Cache\VolatileCache.cs" />
     <Compile Include="Console\ConsoleWriter.cs" />
     <Compile Include="Data\Extensions\SqlCommandExt.cs" />
+    <Compile Include="Object\Singleton.cs" />
+    <Compile Include="Object\WeakReference.cs" />
     <Compile Include="Reflection\TypePropertyLocator.cs" />
     <Compile Include="Value\Result.cs" />
     <Compile Include="Xml\Extensions\XmlNodeExt.cs" />

+ 73 - 0
Common/Object/Singleton.cs

@@ -0,0 +1,73 @@
+namespace Quadarax.Foundation.Common.Object
+{
+    namespace Bee.Core.Pattern
+    {
+        /// <summary>
+        /// Well-known singleton pattern implementation.
+        /// </summary>
+        /// <remarks>
+        /// The singleton type must inherit this class. The actual instance type is determined
+        /// by the only type parameter. The singleton type must implement a default constructor.
+        /// </remarks>
+        /// <typeparam name="TInstance"></typeparam>
+        public class Singleton<TInstance> where TInstance : class, new()
+        {
+            private static volatile TInstance _instance;
+            private static readonly object _syncRoot = new object();
+
+            /// <summary>
+            /// Singleton instance
+            /// </summary>
+            public static TInstance Instance
+            {
+                get
+                {
+                    if (_instance == null)
+                    {
+                        lock (_syncRoot)
+                        {
+                            if (_instance == null)
+                                _instance = new TInstance();
+                        }
+                    }
+
+                    return _instance;
+                }
+            }
+        }
+
+        /// <summary>
+        /// Well-known singleton pattern implementation.
+        /// </summary>
+        /// <remarks>
+        /// The singleton type must inherit this class. The actual instance type is determined
+        /// by the only type parameter. The singleton type must implement a default constructor.
+        /// </remarks>
+        /// <typeparam name="TInstance"></typeparam>
+        /// <typeparam name="TInterface"></typeparam>
+        public class Singleton<TInterface, TInstance>
+            where TInstance : TInterface, new()
+            where TInterface : class
+        {
+            private static volatile TInterface _instance;
+            private static readonly object _syncRoot = new object();
+
+            public static TInterface Instance
+            {
+                get
+                {
+                    if (_instance == null)
+                    {
+                        lock (_syncRoot)
+                        {
+                            if (_instance == null)
+                                _instance = new TInstance();
+                        }
+                    }
+
+                    return _instance;
+                }
+            }
+        }
+    }
+}

+ 32 - 0
Common/Object/WeakReference.cs

@@ -0,0 +1,32 @@
+using System;
+
+namespace Quadarax.Foundation.Common.Object
+{
+    /// <summary>
+    /// Strongly typed version of <see cref="WeakReference"/>.
+    /// </summary>
+    /// <typeparam name="TReference"></typeparam>
+    public class WeakReference<TReference> : WeakReference 
+        where TReference : class
+    {
+        public WeakReference(TReference target)
+            : base(target)
+        {
+        }
+
+        public WeakReference(TReference target, bool trackResurrection)
+            : base(target, trackResurrection)
+        {
+        }
+
+        /// <summary>
+        /// Strongly typed version of <see cref="WeakReference.Target"/>.
+        /// </summary>
+        public new TReference Target
+        {
+            get { return base.Target as TReference; }
+            set { base.Target = value; }
+        }
+    }
+
+}

+ 2 - 2
Common/Properties/AssemblyInfo.cs

@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
 // You can specify all the values or you can default the Build and Revision Numbers
 // by using the '*' as shown below:
 // [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.5")]
-[assembly: AssemblyFileVersion("1.0.0.5")]
+[assembly: AssemblyVersion("1.0.0.6")]
+[assembly: AssemblyFileVersion("1.0.0.6")]

+ 6 - 6
Common/QDR.FND.Common.nuspec

@@ -2,7 +2,7 @@
 <package>
   <metadata>
     <id>Quadarax.Foundation.Common</id>
-    <version>1.0.0.5</version>
+    <version>1.0.0.6</version>
     <title>Quadarax.Foundation.Common</title>
     <authors>Dalibor Votruba, Quadarax</authors>
     <owners>Dalibor Votruba</owners>
@@ -11,13 +11,13 @@
     <requireLicenseAcceptance>false</requireLicenseAcceptance>
     <description>Implementations of simple patterns and misc. extensions.</description>
     <releaseNotes>
-      23.12.2019 v.1.0.0.5
+      27.12.2019 v.1.0.0.6
       New
-      - Add TypePropertyLocator helper
-      - Add Result class (in Value branch)
-      - Add ConsoleWriter class (to write colorful texts to console)
+      - Add Singleton implementation
+      - Add WeakReference generic pattern
+      - Add VolatileCache implementation
     </releaseNotes>
-    <copyright>Copyright (c) 2019 Quadarax</copyright>
+    <copyright>Copyright (c) 2020 Quadarax</copyright>
     <tags>quadarax, foundation, common, qdr, fnd, library, extensions</tags>
     <dependencies>
          </dependencies>

+ 6 - 0
Common/ReleaseNote.txt

@@ -1,3 +1,9 @@
+27.12.2019 v.1.0.0.6
+New
+ - Add Singleton implementation
+ - Add WeakReference generic pattern
+ - Add VolatileCache implementation
+
 23.12.2019 v.1.0.0.5
 New
  - Add TypePropertyLocator helper