Exemple #1
0
        public async Task <bool> SaveDataAsync(string sessionId, IDictionary <string, object> data)
        {
            try
            {
                bool isSaved = false;
                await Task.Run(() =>
                {
                    lock (_sessionDataCache)
                    {
                        _sessionDataCache[sessionId] = new SessionCacheObject(DateTime.Now, data);

                        var filePath    = GetFilePath(sessionId);
                        var sessionData = JsonConvert.SerializeObject(data);
                        File.WriteAllText(filePath, sessionData);

                        isSaved = true;
                    }
                });

                return(isSaved);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemple #2
0
        public async Task <Dictionary <string, object> > GetDataAsync(string sessionId)
        {
            try
            {
                Dictionary <string, object> data = null;

                await Task.Run(() =>
                {
                    lock (_sessionDataCache)
                    {
                        if (_sessionDataCache.ContainsKey(sessionId))
                        {
                            _sessionDataCache[sessionId].LastAccessTime = DateTime.Now;
                            data = (Dictionary <string, object>)_sessionDataCache[sessionId].SessionData;
                        }
                        else if (File.Exists(GetFilePath(sessionId)))
                        {
                            var fileData = File.ReadAllText(GetFilePath(sessionId));
                            data         = new Dictionary <string, object>();

                            if (!string.IsNullOrEmpty(fileData))
                            {
                                data = JsonConvert.DeserializeObject <Dictionary <string, object> >(fileData);
                            }

                            _sessionDataCache[sessionId] = new SessionCacheObject(DateTime.Now, data);
                        }
                    }
                });

                return(data);
            }
            catch (Exception)
            {
                return(null);
            }
        }