using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CRVM.Utility
{



    static public class RedisDB
    {
        static private ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost:6379");
        static private IDatabase db;
        static int errorTimes = 0;

        static RedisDB()
        {
            reconnection();
        }
        static private bool reconnection()
        {
            try
            {
                db = redis.GetDatabase();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        static public void dBStringSet(string key, string value)
        {
            if (key == null || value == null)
            {
                return;
            }
            try
            {
                db.StringSet(key, value);
            }
            catch (Exception ex)
            {
                errorTimes++;
                if (errorTimes > 50)
                {
                    errorTimes = 0;
                    reconnection();
                }
                DevelopLog.DeBug.WriteLogFile("dBStringSet", ex.ToString());
                return;
            }
        }
        static public void dBStringSet(string key, long value)
        {
            if (key == null)
            {
                return;
            }
            try
            {
                db.StringSet(key, value);
            }
            catch (Exception ex)
            {
                errorTimes++;
                if (errorTimes > 50)
                {
                    errorTimes = 0;
                    reconnection();
                }
                DevelopLog.DeBug.WriteLogFile("dBStringSet", ex.ToString());
                return;
            }
        }
    }
}
