You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KevDrawServer
{
public class GlobalSession
{
/// <summary>
/// 存储共享数据的全局字典
/// </summary>
private static readonly Dictionary<string, object> sharedData = new Dictionary<string, object>();
/// <summary>
/// Stores the data.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="data">The data.</param>
public static void StoreData(string key, object data)
{
lock (sharedData)
{
sharedData[key] = data;
}
}
/// <summary>
/// Gets the data.
/// </summary>
/// <param name="key">The key.</param>
/// <returns>An object.</returns>
public static object GetData(string key)
{
lock (sharedData)
{
return sharedData.TryGetValue(key, out object data) ? data : null;
}
}
public static bool ContainsKey(string key)
{
return sharedData.ContainsKey(key);
}
/// <summary>
/// Tries the get value.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
/// <returns>A bool.</returns>
public static bool TryGetValue(string key, out object value)
{
lock (sharedData)
{
return sharedData.TryGetValue(key, out value);
}
}
/// <summary>
/// 删除数据.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="data">The data.</param>
/// <returns>A bool.</returns>
public static bool TryRemove(string key, out object data)
{
lock (sharedData)
{
bool bFound = sharedData.TryGetValue(key, out data);
if (bFound)
{
sharedData.Remove(key);
return true;
}
}
return false;
}
}
}