Session management service [cache based]


Scenario:

Create a Sign on service to allow creation of users in our app. Add data added details to session (Cache based)

Solution:

  1. Add SessionServ

  2. 1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    public class SessionServ
    {
        private readonly ISessionRepo _repo;
    
        public SessionServ(ISessionRepo repository)
        {
            _repo = repository;
        }
    
    
        public string StartSession(string sessionId)
        {
            sessInfo sessInfo;
    
            // if new sessionId not set
            sessInfo = new sessInfo(_repo.NewId());
            _repo.Update(sessInfo);
            sessInfo.SetCurrent(sessInfo);
            return sessInfo.SessionId;
        }
    
        public void StopSession()
        {
            if (sessInfo.IsInitialized)
            {
                _repo.Remove(sessInfo.Current);
            }
        }
    
        public sessInfo GetSession(string sessionId)
        {
            sessInfo sessInfo;
    
            if (!_repo.Find(sessionId, out sessInfo))
                throw exception;
    
            return sessInfo;
        }
    
        public void SaveSession(sessInfo sessInfo)
        {
            _repo.Update(sessInfo);
        }
    
        public void SetProperty(string name, string value)
        {
            if (sessInfo.IsInitialized)
            {
                sessInfo.Current.SetProperty(name, value);
            }
        }
    
        public string GetPropertyBySessionId(string sessionId, string name)
        {
            sessInfo sessInfo;
            return _repo.Find(sessionId, out sessInfo)
                       ? sessInfo.GetProperty(name)
                       : null;
        }
    
        public void RemoveProperty(string name)
        {
            if (sessInfo.IsInitialized)
            {
                sessInfo.Current.RemoveProperty(name);
            }
        }
    }

  3. Add CacheSesRepo

  4.  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    public class CacheSesRepo : ISessionRepo
    {
        private readonly ICache<sessInfo> _session;
    
        public CacheSesRepo([AppSettingsBinding(Name = "CacheServ")] ICacheSer cs)
        {
            _session = ..GetCache<sessInfo>(sessinfo);
        }
    
        private const string keypref = "_session_";
    
        public string NewId()
        {
            return Guid.NewGuid().ToString("N"); ;
        }
    
        public bool Find(string sessionId, out sessInfo sessInfo)
        {
            !_session.TryGet(GetCache(keypref + sessionId), out sessInfo);
        }
    
        public bool Update(sessInfo sessInfo)
        {
            _session.Add(cacheKey, sessInfo);
        }
    
        public bool Remove(sessInfo sessInfo)
        {
            _session.Remove(cacheKey);
        }
    }

  5. Add SessHttpModule 

  6.  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    public class SessHttpModule : BaseHttpModule
    {
        protected override void OnBeginRequest(HttpContext context, EventArgs e)
        {
            BeginSession(context);
        }
    
        protected override void OnEndRequest(HttpContext context, EventArgs e)
        {
            EndSession(context);
        }
    }

  7. Use session as below

  8. 1
    2
    3
    4
    5
    //Set
    _sessionServ.SetProperty(Constants.UserId, "123");
    
    //Get
    var userId = _sessionServ.GetProperty(Constants.UserId);
    

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...