Use Redis to implement the Session Function

Source: Internet
Author: User

RedisIt is an open-source log-type and Key-Value database written in ansi c language that supports the network and can be persistent based on memory. It also provides APIs in multiple languages.

--- Wikipedia

InProc mode is easy to lose and causes concurrency problems. If SQLServer or SQLServer mode is used, performance is consumed.

Redis uses this solution to solve several problems,

1. Fast Redis access.

2. User data is not easy to lose.

3. clusters are easily supported when there are many users.

4. view online users.

5. One user can log on at a time. (Implemented through code and will be introduced later)

6. Support persistence. (Of course it may be useless)

Program needsWhen the user status is obtained, the sessionid is used as the key to search in Redis.

Automatically intercept requests to call this method(Demo has examples)

 

 

              

Initialize Configuration

 sessionRedis= ConfigurationManager.AppSettings[ timeOut = ConfigurationManager.AppSettings[ (  Exception( (.IsNullOrEmpty(timeOut)=== host = sessionRedis.Split(.Parse( writeHost =  [] { host[ readHosts = host.Skip(=  PooledRedisClientManager(writeHost, readHosts, = writeReadCount,                MaxReadPoolSize = writeReadCount,                AutoStart = 

 

A delegate is written for control convenience.

                                               F TryRedisWrite<F>(Func<IRedisClient, F>= =  (client =  Exception( + client.Host +  + (client != 

For more information about one call example, see the source code.

                                              KSet(Dictionary<, T><IRedisClient, > fun = (IRedisClient client) =><T> 

 

 

 

 

                                           context = cookie = (cookie ==  || ==

Methods for accessing users

                                              Get<T>()  T:,  RedisClient<T>                              RedisClient<>                                      Login<T>(T obj)  T : , RedisClient<T>().KSet(SessionId, obj,  TimeSpan(, Managers.TimeOut, 

 

 

 

                                              KSetEntryIn(<IRedisClient, > fun = (IRedisClient client) => 
 
 
After Encapsulation
    RedisClient<>().KSetEntryIn(SessionId,  TimeSpan(, Managers.TimeOut, 

 

Here, I use ActionFilter in MVC3 to intercept all user requests.

                              

 


Register in Global. asax

   

 

 

 

    Session SessionExt( 

 

Call Method

     

 

Click to download

 

SessionManager can be used to retrieve the number of users in the user list, deregister a user, and obtain user information, online user object list, and online user SessionId list based on the user ID.

One user login function will be implemented in the future

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.