csharp-sdk-upm/Engine/LCEngineRequestContext.cs

75 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
using LeanCloud.Storage;
namespace LeanCloud.Engine {
public class LCEngineRequestContext {
public const string RemoteAddressKey = "__remoteAddressKey";
public const string SessionTokenKey = "__sessionToken";
public const string CurrentUserKey = "__currentUser";
private static ThreadLocal<Dictionary<string, object>> requestContext = new ThreadLocal<Dictionary<string, object>>();
public static void Init() {
if (requestContext.IsValueCreated) {
requestContext.Value.Clear();
}
requestContext.Value = new Dictionary<string, object>();
}
public static void Set(string key, object value) {
if (!requestContext.IsValueCreated) {
requestContext.Value = new Dictionary<string, object>();
}
requestContext.Value[key] = value;
}
public static object Get(string key) {
if (!requestContext.IsValueCreated) {
return null;
}
return requestContext.Value[key];
}
public static string RemoteAddress {
get {
object remoteAddress = Get(RemoteAddressKey);
if (remoteAddress != null) {
return remoteAddress as string;
}
return null;
}
set {
Set(RemoteAddressKey, value);
}
}
public static string SessionToken {
get {
object sessionToken = Get(SessionTokenKey);
if (sessionToken != null) {
return sessionToken as string;
}
return null;
}
set {
Set(SessionTokenKey, value);
}
}
public static LCUser CurrentUser {
get {
object currentUser = Get(CurrentUserKey);
if (currentUser != null) {
return currentUser as LCUser;
}
return null;
}
set {
Set(CurrentUserKey, value);
}
}
}
}