123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PMS.Plugins.Common
- {
- public static class HashtableHelper
- {
- private static Hashtable ht = Hashtable.Synchronized(new Hashtable());
- private static object obj = new object();
- /// <summary>
- /// 添加一个缓存项
- /// </summary>
- /// <param name="key">关键字 Key</param>
- /// <param name="data">数据值</param>
- public static void AddCacheIitems(string key, object data)
- {
- try
- {
- if (key != "")
- {
- lock (ht.SyncRoot) //zxm20140212添加
- {
- if (ht[key] != null)
- ht.Remove(key);
- ht.Add(key, data);
- }
- }
- }
- catch (Exception)
- {
- }
- }
- /// <summary>
- /// 获取Hashtable键值
- /// </summary>
- /// <param name="key">key</param>
- /// <returns></returns>
- public static object GetCacheIitems(string key)
- {
- if (key != "")
- {
- lock (ht.SyncRoot)
- {
- return ht[key];
- }
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 移除Hashtable键值
- /// </summary>
- /// <param name="key">key</param>
- public static void RemoveCacheItems(string key)
- {
- if (key != "")
- {
- lock (ht.SyncRoot)
- {
- ht.Remove(key);
- }
- }
- }
- /// <summary>
- /// 移除所有缓存值
- /// </summary>
- public static void RemoveAllCacheItems()
- {
- lock (ht.SyncRoot)
- {
- ht.Clear();
- }
- }
- public static string GetHashtableCount(string key)
- {
- string r = "【insureSys:" + ht[key];
- return r + "|Hashtable键值总和:" + ht.Count + "】";
- }
- /// <summary>
- /// 批量移除缓存
- /// </summary>
- /// <param name="key"></param>
- public static void BatchRemoveCacheItems(string key)
- {
- lock (ht.SyncRoot)
- {
- foreach (DictionaryEntry h in ht)
- {
- string keyName = h.Key.ToStringEx();
- if (keyName.Contains(key))
- {
- ht.Remove(keyName);
- }
- }
- }
- }
- /// <summary>
- /// 将DataTable 转换hashTable
- /// </summary>
- /// <param name="dt"></param>
- /// <returns></returns>
- public static Hashtable DataTableToHashTable(DataTable dt)
- {
- DataRow row = dt.Rows[0];
- Hashtable hash = new Hashtable();
- for (int i = 0; i < row.Table.Columns.Count; i++)
- {
- if (row[row.Table.Columns[i].ColumnName].ToStringEx() != "")
- hash.Add(row.Table.Columns[i].ColumnName, row[row.Table.Columns[i].ColumnName].ToStringEx());
- }
- return hash;
- }
- /// <summary>
- /// 获取一个6位验证码随机数
- /// </summary>
- /// <returns></returns>
- private static int NewRandom()
- {
- lock (obj)
- {
- Random r = new Random(System.Environment.TickCount);
- int i = r.Next(100000, 999999);
- return i;
- }
- }
- /// <summary>
- /// 新生成一个Key
- /// </summary>
- /// <returns></returns>
- public static string NewKey()
- {
- string guid = NewRandom().ToStringEx();//Guid.NewGuid().ToString().ToUpper();
- return guid;
- }
- }
- }
|