• Redis 使用之StackExchange.Redis


    1》设置与连接StackExchange.Redis 

    public static ConnectionMultiplexer RedisConnection;
    public static IDatabase RedisCacheDb;
    
    protected void Session_Start(object sender, EventArgs s)
    {
        string RedisConn = ConfigurationManager.ConnectionStrings["RedisCache"].ConnectionString;
        if (!string.IsNullOrEmpty(RedisConn))
        {
            if (RedisConnection == null || !RedisConnection.IsConnected)
                RedisConnection = ConnectionMultiplexer.Connect(RedisConn);
            RedisCacheDb = RedisConnection.GetDatabase();
        }
    }
    private static Lazy<ConfigurationOptions> configOptions = new Lazy<ConfigurationOptions>
      (() => { var configOptions = new ConfigurationOptions(); configOptions.EndPoints.Add("localhost:6379"); configOptions.ClientName = "RedisName"; configOptions.ConnectTimeout = 100000; configOptions.SyncTimeout = 100000; return configOptions; }); private static ConnectionMultiplexer conn; private static ConnectionMultiplexer LeakyConn { get { if (conn == null || !conn.IsConnected) conn = ConnectionMultiplexer.Connect(configOptions.Value); return conn; } }

     2》使用StackExchange.Redis

    static void Main(string[] args)
    {
    
           IDatabase db = BaseDataRedis.LeakeyConn.GetDatabase();
           string key = "String";
           string value = "value";
           db.KeyDelete(key);
           Console.WriteLine("Before the assignment:{0}", db.StringGet(key));
           db.StringSet(key, value);
           Console.WriteLine("when a value assigned:{0}", db.StringGet(key));
    
    
           string[] companies = new string[] { "Google", "Apple", "Amazon", "GFI", "Blizzard", "IBM" };
    
           int[] companyScores = new int[] { 95, 15, 80, 0, 100, 56 };
    
           key = "awesomecompanies";
           db.KeyDelete(key);
           for (int i = 0; i < companies.Length; i++)
           {
               db.SortedSetAdd(key, companies[i], companyScores[i]);
           }
    
           RedisValue[] redisValue = db.Sort(key);
    
           for (int i = 0; i < redisValue.Length; i++)
           {
                Console.WriteLine("Redis Key:{0},companiesName:{1}", key, redisValue[i].ToString());
           }
    
           Console.ReadKey();
    }

     

  • 相关阅读:
    System.Runtime.InteropServices.COMException: 拒绝
    Struts中Action里对Form的运用理解
    Structs中<logic:present><logic:iterator>的使用
    AspxGridView控件自写数据源时出现删错行的问题
    AspxGridView绑定自主数据源的操作
    水晶报表发布后logon failed,Error Code:ox
    lock skew detected. Your build may be incomplete
    linux ulimit调优
    Erlang服务器内存耗尽bug跟踪过程
    erlang lists 系列函数功能与用法详解(共68个函数)
  • 原文地址:https://www.cnblogs.com/david_king/p/5363553.html
Copyright © 2020-2023  润新知