Procházet zdrojové kódy

mxg:对下拉列表的数据源添加缓存

mengxiangge před 6 roky
rodič
revize
20d919f9b5

+ 63 - 0
MBI/SAGA.DotNetUtils/Cache/MemoryCacheHelper.cs

@@ -0,0 +1,63 @@
+/* ==============================================================================
+ * 功能描述:使用MemoryCache进行缓存
+ * 创 建 者:Garrett
+ * 创建日期:2018/12/21 17:29:40
+ * ==============================================================================*/
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.Caching;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SAGA.DotNetUtils.Cache
+{
+    /// <summary>
+    /// 基于MemoryCache的缓存辅助类
+    /// </summary>
+    public static class MemoryCacheHelper
+    {
+        private static readonly Object _locker = new object();
+
+        public static T GetCacheItem<T>(String key, Func<T> cachePopulate, TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null)
+        {
+            if (String.IsNullOrWhiteSpace(key)) throw new ArgumentException("Invalid cache key");
+            if (cachePopulate == null) throw new ArgumentNullException("cachePopulate");
+            if (slidingExpiration == null && absoluteExpiration == null) throw new ArgumentException("Either a sliding expiration or absolute must be provided");
+
+            if (MemoryCache.Default[key] == null)
+            {
+                lock (_locker)
+                {
+                    if (MemoryCache.Default[key] == null)
+                    {
+                        var item = new CacheItem(key, cachePopulate());
+                        var policy = CreatePolicy(slidingExpiration, absoluteExpiration);
+
+                        MemoryCache.Default.Add(item, policy);
+                    }
+                }
+            }
+
+            return (T)MemoryCache.Default[key];
+        }
+
+        private static CacheItemPolicy CreatePolicy(TimeSpan? slidingExpiration, DateTime? absoluteExpiration)
+        {
+            var policy = new CacheItemPolicy();
+
+            if (absoluteExpiration.HasValue)
+            {
+                policy.AbsoluteExpiration = absoluteExpiration.Value;
+            }
+            else if (slidingExpiration.HasValue)
+            {
+                policy.SlidingExpiration = slidingExpiration.Value;
+            }
+
+            policy.Priority = CacheItemPriority.Default;
+
+            return policy;
+        }
+    }
+}

+ 2 - 0
MBI/SAGA.DotNetUtils/SAGA.DotNetUtils.csproj

@@ -179,6 +179,7 @@
     <Reference Include="System.Drawing" />
     <Reference Include="System.IO.Compression" />
     <Reference Include="System.Management" />
+    <Reference Include="System.Runtime.Caching" />
     <Reference Include="System.Runtime.Serialization" />
     <Reference Include="System.ServiceModel" />
     <Reference Include="System.Transactions" />
@@ -196,6 +197,7 @@
   <ItemGroup>
     <Compile Include="AOP\AopAttribute.cs" />
     <Compile Include="AOP\IAop.cs" />
+    <Compile Include="Cache\MemoryCacheHelper.cs" />
     <Compile Include="Component\AllowDisposableSet!1.cs" />
     <Compile Include="Component\ChangedEventArgs.cs" />
     <Compile Include="Component\ChangedEventHandler.cs" />

+ 1 - 1
MBI/SAGA.MBI/CmbData/FloorCmbVm.cs

@@ -1,5 +1,5 @@
 /* ==============================================================================
- * 功能描述:ProvinceCityVm  
+ * 功能描述:
  * 创 建 者:Garrett
  * 创建日期:2018/4/17 14:34:03
  * ==============================================================================*/

+ 34 - 0
MBI/SAGA.MBI/Common/CacheAspect.cs

@@ -0,0 +1,34 @@
+/* ==============================================================================
+ * 功能描述:AOP缓存
+ * 创 建 者:Garrett
+ * 创建日期:2018/12/21 17:33:03
+ * ==============================================================================*/
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using ArxOne.MrAdvice.Advice;
+using SAGA.DotNetUtils.Cache;
+
+namespace SAGA.MBI.Common
+{
+    /// <summary>
+    /// CacheAspect
+    /// </summary>
+    class CacheAspect : Attribute, IMethodAdvice
+    {
+        public void Advise(MethodAdviceContext context)
+        {
+            string key = $"{context.TargetType.FullName}.{context.TargetMethod.Name}.{string.Join(".", context.Parameters.Cast<string>())}";
+            var result = MemoryCacheHelper.GetCacheItem(key,
+                delegate ()
+                {
+                    context.Proceed();
+                    return context.ReturnValue;
+                },
+                new TimeSpan(0, 30, 0));//30分钟过期
+            context.ReturnValue = result;
+        }
+    }
+}

+ 1 - 0
MBI/SAGA.MBI/DataArrange/DalCmd.cs

@@ -14,6 +14,7 @@ using SAGA.DotNetUtils;
 using SAGA.DotNetUtils.Extend;
 using SAGA.DotNetUtils.WPF.UserControl.ComboboxTreeView;
 using SAGA.MBI.CmbData;
+using SAGA.MBI.Common;
 
 namespace SAGA.MBI.DataArrange
 {

+ 10 - 0
MBI/SAGA.MBI/JsonConvert/CmbDataSourceConvert.cs

@@ -30,6 +30,7 @@ namespace SAGA.MBI.JsonConvert
         /// 查询地理信息--省市信息
         /// </summary>
         /// <returns></returns>
+        [CacheAspect]
         public static List<ProvinceInfo> GeographyDic()
         {
             List<ProvinceInfo> list = new List<ProvinceInfo>();
@@ -116,6 +117,7 @@ namespace SAGA.MBI.JsonConvert
         /// 查询地理信息--省市信息,使用TreeNodeItem形式表达
         /// </summary>
         /// <returns></returns>
+        [CacheAspect]
         public static List<CMBTreeNodeItem<ICMBTreeNodeItem>> GeographyTree()
         {
             List<CMBTreeNodeItem<ICMBTreeNodeItem>> list = new List<CMBTreeNodeItem<ICMBTreeNodeItem>>();
@@ -160,6 +162,7 @@ namespace SAGA.MBI.JsonConvert
         /// 气候区
         /// </summary>
         /// <returns></returns>
+        [CacheAspect]
         public static List<ClimaticRegion> ClimateDic()
         {
             List<ClimaticRegion> list = new List<ClimaticRegion>();
@@ -191,6 +194,7 @@ namespace SAGA.MBI.JsonConvert
         /// 气候区
         /// </summary>
         /// <returns></returns>
+        [CacheAspect]
         public static List<CMBTreeNodeItem<ICMBTreeNodeItem>> ClimateTree()
         {
             List<CMBTreeNodeItem<ICMBTreeNodeItem>> list = new List<CMBTreeNodeItem<ICMBTreeNodeItem>>();
@@ -212,6 +216,7 @@ namespace SAGA.MBI.JsonConvert
         /// 建筑功能类型
         /// </summary>
         /// <returns></returns>
+        [CacheAspect]
         public static List<CMBTreeNodeItem<ICMBTreeNodeItem>> BuildFuncTypeDic()
         {
             List<CMBTreeNodeItem<ICMBTreeNodeItem>> list = new List<CMBTreeNodeItem<ICMBTreeNodeItem>>();
@@ -246,6 +251,7 @@ namespace SAGA.MBI.JsonConvert
         /// </summary>
         /// <param name="infoPointCode"></param>
         /// <returns></returns>
+        [CacheAspect]
         public static List<CMBTreeNodeItem<ICMBTreeNodeItem>> BuildingInfoDS(string infoPointCode)
         {
             MInfoCode infoCode = BuildingDic().FirstOrDefault(t => t.InfoPointCode == infoPointCode);
@@ -255,6 +261,7 @@ namespace SAGA.MBI.JsonConvert
         /// 建筑体的数据字典
         /// </summary>
         /// <returns></returns>
+        [CacheAspect]
         public static List<MInfoCode> BuildingDic()
         {
             List<MInfoCode> list = new List<MInfoCode>();
@@ -276,6 +283,7 @@ namespace SAGA.MBI.JsonConvert
         /// </summary>
         /// <param name="infoPointCode"></param>
         /// <returns></returns>
+        [CacheAspect]
         public static List<CMBTreeNodeItem<ICMBTreeNodeItem>> FloorInfoDS(string infoPointCode)
         {
             List<MInfoCode> list = FloorDic();
@@ -286,6 +294,7 @@ namespace SAGA.MBI.JsonConvert
         /// 楼层的数据字典
         /// </summary>
         /// <returns></returns>
+        [CacheAspect]
         public static List<MInfoCode> FloorDic()
         {
             List<MInfoCode> list = new List<MInfoCode>();
@@ -307,6 +316,7 @@ namespace SAGA.MBI.JsonConvert
         /// 设备种族类型编码
         /// </summary>
         /// <returns></returns>
+        [CacheAspect]
         public static List<MEquipmentFamily> EquipmentFamilyDic()
         {
             List<MEquipmentFamily> list = new List<MEquipmentFamily>();

+ 1 - 0
MBI/SAGA.MBI/SAGA.MBI.csproj

@@ -241,6 +241,7 @@
     <Compile Include="CmbData\ProvinceCityInfo.cs" />
     <Compile Include="CmbData\BuildingCmbVm.cs" />
     <Compile Include="CmbData\ProvinceCityVm.cs" />
+    <Compile Include="Common\CacheAspect.cs" />
     <Compile Include="Html5Command.cs" />
     <Compile Include="Command.cs" />
     <Compile Include="Common\MBIAssistHelper.cs" />