[Redis]c# redis緩衝輔助類

來源:互聯網
上載者:User

標籤:cep   ges   資料   ima   lex   password   設定   tle   name   

using NServiceKit.Redis;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace H5.Utility{    /// <summary>    /// 分布式緩衝    /// </summary>    public static class RedisCache    {        private static IRedisClient RCClient = null;        /// <summary>        /// 設定redis緩衝        /// </summary>        /// <typeparam name="T">泛型類</typeparam>        /// <param name="key">緩衝鍵</param>        /// <param name="value">泛型實體</param>        /// <param name="expire">到期時間</param>        /// <returns></returns>        public static bool Set<T>(string key, T value, DateTime expire)        {            try            {                using (RCClient = GetClient())                {                    return RCClient.Set<T>(key, value, expire);                }            }            catch            {                return false;            }        }        /// <summary>        /// 擷取緩衝        /// </summary>        /// <typeparam name="T">實體</typeparam>        /// <param name="key">索引值</param>        /// <returns></returns>        public static T Get<T>(string key)        {            try            {                using (RCClient = GetClient())                {                    return RCClient.Get<T>(key);                }            }            catch            {                //如果redis出現異常,則直接返回預設值                return default(T);            }        }        /// <summary>        /// 移除緩衝        /// </summary>        /// <param name="key"></param>        public static void Remove(string key)        {            using (RCClient = GetClient())            {                RCClient.Remove(key);            }        }        /// <summary>        /// 擷取用戶端        /// </summary>        /// <returns></returns>        private static IRedisClient GetClient()        {            RedisClientFactory factory = RedisClientFactory.Instance;            RedisClient client = null;            if (string.IsNullOrEmpty(ConfigManager.RedisServer))            {                throw new ArgumentNullException("redis server ip is empty.");            }            if (string.IsNullOrEmpty(ConfigManager.RedisPwd))            {                throw new ArgumentNullException("redis server pwd is empty.");            }            client = factory.CreateRedisClient(ConfigManager.RedisServer, ConfigManager.RedisPort);            client.Password = ConfigManager.RedisPwd;            client.Db = ConfigManager.RedisServerDb;            return client;        }    }}

 用到的程式集

功能描述

可以直接緩衝實體類,設定到期時間,移除緩衝,擷取緩衝功能。

使用RedisClientFactory工廠擷取redis用戶端執行個體。如果Redis設定了密碼,在設定檔中添加修改

            client = factory.CreateRedisClient(ConfigManager.RedisServer, ConfigManager.RedisPort);            client.Password = ConfigManager.RedisPwd;

修改redis的ip和連接埠號碼,密碼即可。

使用情境

具體的使用過程中,使用redis的逾時可以對資料進行一些持久化管理,對於一些資料一致性不高的資料進行緩衝,使得讀取速度提高,使用redis叢集時可以是用主從複製功能,Redis叢集沒有中心節點,並且帶有複製和容錯移轉特性,這可以避免單個節點成為效能瓶頸,或者因為某個節點下線而導致整個叢集下線。

轉載:部落格地址:http://www.cnblogs.com/wolf-sun/

[Redis]c# redis緩衝輔助類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.