Cache of high-performance website architecture design (1) - Redis C# client

1. What is Redis

REmote DIctionary Server, or Redis for short, is a Key-Value storage system similar to Memcached. Compared with Memcached, it supports richer data structures, including string (string), list (linked list), set (collection), zset (sorted set -- ordered collection) and hash (hash type), and provides Data persistence mechanism, in some scenarios, you can use it as a non-relational database. It is a high-performance storage system that supports over 100K+ read and write frequencies per second. At the same time, it also supports the publish/subscribe of messages, which gives you another option when building a high-performance message queuing system.

2. Download

Click here to download .

What I downloaded here is the redis-3.0.0-beta5 version, and redis supports clustering since 3.0.
3. Installation

The following installation steps are all on the OSX operating system:

1. Find the redis-3.0.0-beta5.tar.gz file you just downloaded, and decompress the file.

2. Copy the decompressed folder to a directory you can easily find, and change the folder name to redis, because the version number is too long.

2. Open the terminal and enter the redis folder. The command is as follows:

Last login: Fri May 30 21:33:25 on ttys000

zhaoguihuadediannao:~ zhaogh$ cd applications/dev/redis

zhaoguihuadediannao:redis zhaogh$ 

3. Enter the make command at the command prompt and wait for a while to complete the installation. If the system prompts that the make command cannot be found, please refer to this article.

Installed, is not a little excited. Have a cigarette and let's move on.

Fourth, use

1. Start the service, first enter the src directory, and then execute redis-server.

zhaoguihuadediannao:redis zhaogh$ cd src

zhaoguihuadediannao:src zhaogh$ ./redis-server

You will see:

2343:M 30 May 21:42:50.741 # Server started, Redis version 2.9.54

2343:M 30 May 21:42 :50.741 * The server is now ready to accept connections on port 6379

, I read it right, TMD I clearly downloaded 3.0.0, how to display Redis version 2.9.54? Well, this is not the point, I am too lazy to pursue it.

6379 is the default port of redis, you will know how to modify this default port in subsequent articles.

2. Client connection.

You will find that after the fourth step, we can no longer execute other commands, what should we do? Because redis monopolizes this process, I will tell you later if it is modified to run in the background.

Let's open another terminal for now. Or enter the src directory first:

zhaoguihuadediannao:~ zhaogh$ cd applications/dev/redis/src

zhaoguihuadediannao:src zhaogh$ 

Enter the ./redis-cli command: zhaoguihuadediannao:src zhaogh

$ ./redis-cli

127.0.0.1:6379>Connected 

up, hahaha.

3. Test several redis commands:

127.0.0.1:6379> set testkey001 testkey001

OK

127.0.0.1:6379> get testkey001

"testkey001"

127.0.0.1:6379> append testkey001 aaa

(integer) 13

127.0.0.1:6379> get testkey001

"testkey001aaa"

127.0.0.1:6379> 
4. Close the connection and execute the quit command

127.0.0.1:6379> quit

zhaoguihuadediannao:src zhaogh$ 

5. Turn off the service.

Since we have just logged out of the client, we connect again and execute the shutdown command:

127.0.0.1:6379> shutdown

A resdis helper class is provided below

 

 /// <summary>  
    /// Redis public auxiliary class library  
    /// </summary>  
    public class RedisHelper : IDisposable
    {
        public RedisClient Redis = new RedisClient("127.0.0.1", 6379);
        //Buffer pool  
        PooledRedisClientManager prcm = new PooledRedisClientManager();
        //Default cache expiration time in seconds  
        public int secondsTimeOut = 30 * 60;

        /// <summary>  
        /// Buffer pool  
        /// </summary>  
        /// <param name="readWriteHosts"> </param>  
        /// <param name="readOnlyHosts"></param>  
        /// <returns></returns>   
        public static PooledRedisClientManager CreateManager(
         string[] readWriteHosts, string[] readOnlyHosts)
        {
            return new PooledRedisClientManager(readWriteHosts, readOnlyHosts,
                new RedisClientManagerConfig
                {
                    MaxWritePoolSize = readWriteHosts.Length * 5,
                    MaxReadPoolSize = readOnlyHosts.Length * 5,
                    AutoStart = true,
                });// { RedisClientFactory = (IRedisClientFactory)RedisCacheClientFactory.Instance.CreateRedisClient("127.0.0.1", 6379) };            
        }


        /// <summary>  
        /// 构造函数  
        /// </summary>  
        /// <param name="openPooledRedis">是否开启缓冲池</param>  
        public RedisHelper(bool openPooledRedis = false)
        {
            if (openPooledRedis)
            {
                prcm = CreateManager(new string[] { "127.0.0.1:6379" }, new string[] { "127.0.0.1:6379" });
                Redis = prcm.GetClient() as RedisClient;
            }
        }

 #region Key/Value storage  
        /// <summary>  
        /// Set cache  
        /// </summary>  
        /// <typeparam name="T"></typeparam>  
        /// <param name="key"> Cache build</param>  
        /// <param name="t">cache value</param>  
        /// <param name="timeout">expiration time, in seconds, -1: not expired, 0: expired by default time</param>  
        /// <returns></returns>  
        public bool Set<T>(string key, T t, int timeout = 0)
        {
            if (timeout >= 0)
            {
                if (timeout > 0)
                {
                    secondsTimeOut = timeout;
                }
                Redis.Expire(key, secondsTimeOut);
            }

            return Redis.Add<T>(key, t);
        }
        /// <summary>  
        /// 获取  
        /// </summary>  
        /// <typeparam name="T"></typeparam>  
        /// <param name="key"></param>  
        /// <returns></returns>  
        public T Get<T>(string key)
        {
            return Redis.Get<T>(key);
        }
        /// <summary>  
        /// 删除  
        /// </summary>  
        /// <param name="key"></param>  
        /// <returns></returns>  
        public bool Remove(string key)
        {
            return Redis.Remove(key);
        }

        public bool Add<T>(string key, T t, int timeout)
        {
            if (timeout >= 0)
            {
                if (timeout > 0)
                {
                    secondsTimeOut = timeout;
                }
                Redis.Expire(key, secondsTimeOut);
            }
            return Redis.Add<T>(key, t);
        }
        #endregion

        #region 链表操作  
        /// <summary>  
        /// 根据IEnumerable数据添加链表  
        /// </summary>  
        /// <typeparam name="T"></typeparam>  
        /// <param name="listId"></param>  
        /// <param name="values"></param>  
        /// <param name="timeout"></param>  
        public void AddList<T>(string listId, IEnumerable<T> values, int timeout = 0)
        {
            Redis.Expire(listId, 60);
            IRedisTypedClient<T> iredisClient = Redis.As<T>();
            if (timeout >= 0)
            {
                if (timeout > 0)
                {
                    secondsTimeOut = timeout;
                }
                Redis.Expire(listId, secondsTimeOut);
            }
            var redisList = iredisClient.Lists[listId];
            redisList.AddRange(values);
            iredisClient.Save();
        }

/// <summary>  
        /// 添加单个实体到链表中  
        /// </summary>  
        /// <typeparam name="T"></typeparam>  
        /// <param name="listId"></param>  
        /// <param name="Item"></param>  
        /// <param name="timeout"></param>  
        public void AddEntityToList<T>(string listId, T Item, int timeout = 0)
        {
            IRedisTypedClient<T> iredisClient = Redis.As<T>();
            if (timeout >= 0)
            {
                if (timeout > 0)
                {
                    secondsTimeOut = timeout;
                }
                Redis.Expire(listId, secondsTimeOut);
            }
            var redisList = iredisClient.Lists[listId];
            redisList.Add(Item);
            iredisClient.Save();
        }
        /// <summary>  
        /// 获取链表  
        /// </summary>  
        /// <typeparam name="T"></typeparam>  
        /// <param name="listId"></param>  
        /// <returns></returns>  
        public IEnumerable<T> GetList<T>(string listId)
        {
            IRedisTypedClient<T> iredisClient = Redis.As<T>();
            return iredisClient.Lists[listId];
        }
        /// <summary>  
        /// 在链表中删除单个实体  
        /// </summary>  
        /// <typeparam name="T"></typeparam>  
        /// <param name="listId"></param>  
        /// <param name="t"></param>  
        public void RemoveEntityFromList<T>(string listId, T t)
        {
            IRedisTypedClient<T> iredisClient = Redis.As<T>();
            var redisList = iredisClient.Lists[listId];
            redisList.RemoveValue(t);
            iredisClient.Save();
        }
        /// <summary>  
        /// 根据lambada表达式删除符合条件的实体  
        /// </summary>  
        /// <typeparam name="T"></typeparam>  
        /// <param name="listId"></param>  
        /// <param name="func"></param>  
        public void RemoveEntityFromList<T>(string listId, Func<T, bool> func)
        {
            using (IRedisTypedClient<T> iredisClient = Redis.As<T>())
            {
                var redisList = iredisClient.Lists[listId];
                T value = redisList.Where(func).FirstOrDefault();
                redisList.RemoveValue(value);
                iredisClient.Save();
            }
        }
        #endregion

 

/// <summary>
        /// 释放资源 
        /// </summary>
        public void Dispose()
        {
            if (Redis != null)
            {
                Redis.Dispose();
                Redis = null;
            }
            GC.Collect();
        }

 

https://www.cnblogs.com/kuangood/p/6858196.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325523641&siteId=291194637