• 简单的容器盒子


    做一个公用,全局共享的容器盒子,全局单利使用。

    不想将数据存放在数据库,数据更新频率很高,并且只做临时存放点,

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Reflection;
      5 using System.Text;
      6 using System.Threading.Tasks;
      7 /*********************************************************************
      8 唯一标识:339980d6-46f1-450f-85ff-d6cfda251f20
      9 版本号:v1.0.0.0
     10 CLR版本:4.0.30319.42000 
     11 ======================================================================
     12 创建人:  liusg
     13 创建时间:2018/7/5 17:05:04
     14 **********************************************************************/
     15 namespace MTDemoRun
     16 {
     17     public class DBContainer
     18     {
     19         private List<PaCrt> list = new List<PaCrt>();
     20         private static DBContainer _siCrt;
     21         public static readonly object _Locked = new object();
     22         public static DBContainer GetSingle()
     23         {
     24             if(_siCrt==null)
     25             {
     26                 lock(_Locked)
     27                 {
     28                     if(_siCrt==null)
     29                     {
     30                         _siCrt = new DBContainer();
     31                     }
     32                 }
     33             }
     34             return _siCrt;
     35         } 
     36         private DBContainer()
     37         {
     38             list = ReadeDB();//初次加载,初始化数据
     39         }
     40         
     41         
     42         public bool Add(PaCrt model)
     43         {
     44             list.Add(model);
     45             return true;
     46         }
     47         /// <summary>
     48         /// 可添加,可修改
     49         /// 存在时候修改,不存在时添加
     50         /// </summary>
     51         /// <param name="mode"></param>
     52         /// <returns></returns>
     53         public bool UpdatOrAdd(PaCrt mode)
     54         {
     55             try
     56             {
     57                 var t = mode.GetType();
     58                 var propert = t.GetProperties().Where(p => p.IsDefined(typeof(KeyCrtAttributers), true))
     59                     .FirstOrDefault();
     60 
     61                 if (propert == null) { return false; } 
     62 
     63                 var d = list.Find(p => p.GetType()
     64                   .GetRuntimeProperty(propert.Name)
     65                   .GetValue(p) == mode.GetType()
     66                   .GetRuntimeProperty(propert.Name)
     67                   .GetValue(mode));
     68 
     69                 if (d != null)
     70                 {
     71                     list.Remove(d);
     72                 }
     73                 list.Add(mode);
     74                 return true;
     75             }
     76             catch (Exception e)
     77             {
     78 
     79                 return fasle;
     80             }
     81         }
     82         public PaCrt GetModel(Func<PaCrt,bool> where)
     83         {
     84             return Clone().Where(where).FirstOrDefault();
     85         }
     86         public PaCrt Find(Func<PaCrt, bool> where)
     87         {
     88             return Clone().Where(where).FirstOrDefault();
     89         }
     90         public bool Delete(PaCrt model)
     91         {
     92             if (model == null) return false;
     93             return list.Remove(model);
     94         }
     95         public List<PaCrt> GetList(Func<PaCrt, bool> where)
     96         {
     97             return Clone().Where(where).ToList();
     98         }
     99         public List<PaCrt> GetList( )
    100         {
    101             return Clone().Where(p=>true).ToList();
    102         }
    103         /// <summary>
    104         /// 取数据时候,取副表的数据
    105         /// </summary>
    106         /// <returns></returns>
    107         private List<PaCrt> Clone()
    108         {
    109             List<PaCrt> newList = new List<PaCrt>();
    110             newList.AddRange(list);
    111             return newList;
    112         }
    113        /// <summary>
    114        /// 写入数据
    115        /// </summary>
    116        /// <param name="dblist"></param>
    117         private void WriteDB(List<PaCrt> dblist)
    118         {
    119       //可写入数据库,也可以存入临时文件中
    120         }
    121         /// <summary>
    122         /// 读取数据
    123         /// 
    124         /// 
    125         /// </summary>
    126         /// <returns></returns>
    127         private List<PaCrt> ReadeDB()
    128         {  //可从数据库中读取,也可以从临时文件中
    129             return new List<PaCrt>();
    130         }
    131     }
    132     /// <summary>
    133     /// Model
    134     /// </summary>
    135     public class PaCrt
    136     {
    137         [KeyCrtAttributers]
    138         public string Name { get; set; }
    139         public int Value { get; set; }
    140     }
    141     public class KeyCrtAttributers : Attribute
    142     {
    143         /// <summary>
    144         /// 作为唯一标识,主键,
    145         /// 出现多个主键时候,只取第一个
    146         /// </summary>
    147         public bool MakeKey { get; set; }
    148         public KeyCrtAttributers()
    149         {
    150             MakeKey = true;   
    151         }
    152     }
    153 
    154 }

    测试的情况如下:

    测试代码:

     1 using Common;
     2 using ManagerAPI;
     3 using MetaTrader4.Manager.Contracts;
     4 using MetaTrader4.Manager.Contracts.Configuration;
     5 using System;
     6 using System.Collections.Generic;
     7 using System.Linq;
     8 using System.Reflection;
     9 using System.Text;
    10 using System.Threading;
    11 using System.Threading.Tasks;
    12 
    13 namespace MTDemoRun
    14 {
    15     class Program
    16     {
    17         static void Main(string[] args)
    18         {
    19             Console.Title = "Liusg";
    20             string[] list = new string[]
    21             {
    22                 "ASD",
    23                 "WSD",
    24                 "AQW",
    25                 "TGH",
    26                 "FGB",
    27                 "JHD",
    28                 "OLK",
    29                 "POU",
    30                 "EFT",
    31                 "SRD",
    32             };
    33              
    34              
    35             var t=DBContainer.GetSingle();
    36             Task.Factory.StartNew(() =>
    37             {
    38                 while (true)
    39                 {
    40                     var td = Guid.NewGuid().ToString();
    41                     t.UpdatOrAdd(new PaCrt
    42                     {
    43                         Name = list[td[0] % 10],
    44                         Value = td[0]
    45                     }); t.UpdatOrAdd(new PaCrt
    46                     {
    47                         Name = list[td[1] % 10],
    48                         Value = td[1]
    49                     }); t.UpdatOrAdd(new PaCrt
    50                     {
    51                         Name = list[td[2] % 10],
    52                         Value = td[2]
    53                     });
    54                     Thread.Sleep(800);
    55                 }
    56             });
    57 
    58             Task.Factory.StartNew(() =>
    59             {
    60                 while (true)
    61                 {
    62                     t.GetList().OrderBy(p=>p.Name).ToList().ForEach(p => Console.Write("{0}:{1},",p.Name,p.Value));
    63                     Console.WriteLine("");
    64                     Thread.Sleep(1000);
    65                 }
    66             });
    67             do
    68             {
    69                 Console.Write("输入t退出:");
    70 
    71             } while (Console.ReadLine() == "t");
    72         }
    73 
    74        
    75     }      
    76 }
    
    
  • 相关阅读:
    如何做实时监控?—— 参考 Spring Boot 实现
    如何做实时监控?—— 参考 Spring Boot 实现
    spring boot application properties配置详解
    Jrebel 6.2.1破解
    智能社-JS -wiki
    hibernate.properties
    Tomcat 的 socket bind failed的解决方法
    js 排序 SORT 各种方法
    java EE 如何使用Eclipse启动一个项目
    2016-06-06 数组的几个重要方法
  • 原文地址:https://www.cnblogs.com/lsgControl/p/9272342.html
Copyright © 2020-2023  润新知