private static RedisClient GetRpcClient() { if (_prcm == null) { CreateManager(); } var client = _prcm.GetClient(); if (client != null && !string.IsNullOrWhiteSpace(_pwd)) { client.Auth(_pwd); } return(client); }
static void Main(string[] args) { using (var redis = new RedisClient("192.168.1.67", 6379)) { string ping = redis.Ping(); var s = redis.Info(); string echo = redis.Echo("hello world"); DateTime time = redis.Time(); } //while (true) { using (var redisPool = new RedisConnectionPool("192.168.1.67", 6379, 50)) { var redis = redisPool.GetClient(); var r = redis.Select(15); var r1 = redis.Set("csredis_netcore", DateTime.Now, 600); var r2 = redis.Get("csredis_netcore"); Console.WriteLine(r2); } } RedisClient redis2 = null; using (var sentinel = new RedisSentinelManager("192.168.1.170:26379")) { //sentinel.Add("192.168.1.400"); // add host using default port //sentinel.Add("192.168.1.400", 36379); // add host using specific port sentinel.Connected += (s, e) => sentinel.Call(x => x.Host); // this will be called each time a master connects sentinel.Connect("session-master"); // open connection var test2 = sentinel.Call(x => x.Time()); // use the Call() lambda to access the current master connection redis2 = sentinel.Call(x => x); var r = redis2.Select(15); var r1 = redis2.Set("csredis_netcore2", DateTime.Now, 600); var r2 = redis2.Get("csredis_netcore2"); var r3 = redis2.Set("csredis_netcore3", DateTime.Now); Console.WriteLine(r2); } //while (true) { var r0 = redis2.Select(15); var r1 = redis2.Set("csredis_netcore4", DateTime.Now); var r2 = redis2.Get("csredis_netcore4"); Console.WriteLine(r2); } Console.ReadKey(); }
public static int GetSeasonId() { if (SeasonId != 0 && Year != 0) { return(SeasonId); } int season = RedisConnectionPool.GetClient().Get <int>(GameConsts.Pvp.PvpSeasonCountKey); int year = RedisConnectionPool.GetClient().Get <int>(GameConsts.Pvp.PvpSeasonOfYearKey); if (season == 0 || year == 0) { SeasonId = 1; Year = DateTime.UtcNow.Year; RedisConnectionPool.GetClient().Set(GameConsts.Pvp.PvpSeasonCountKey, SeasonId); RedisConnectionPool.GetClient().Set(GameConsts.Pvp.PvpSeasonOfYearKey, Year); } return(season); }
private static void OnEntitySyncQueue(object state) { if (Interlocked.CompareExchange(ref _entityQueueRunning, 1, 0) == 0) { try { var tempRemove = Interlocked.Exchange(ref _entityRemoteSet, new HashSet <string>()); var temp = Interlocked.Exchange(ref _entitySet, new HashSet <string>()); if (temp.Count == 0) { return; } using (var client = RedisConnectionPool.GetClient()) { var pipeline = client.CreatePipeline(); try { bool hasPost = false; foreach (var key in temp) { var keyValues = key.Split('_', '|'); if (keyValues.Length != 3) { continue; } AbstractEntity entity = CacheFactory.GetPersonalEntity(key) as AbstractEntity; int id = keyValues[1].ToInt(); string keyCode = keyValues[2]; string redisKey = string.Format("{0}_{1}", keyValues[0], keyCode); string hashId = GetRedisSyncQueueKey(id); byte[] idBytes = BufferUtils.GetBytes(id); var keyBytes = RedisConnectionPool.ToByteKey(redisKey); bool isDelete; byte[] entityBytes; if (entity != null) { isDelete = entity.IsDelete; entityBytes = _serializer.Serialize(entity); } else if (tempRemove.Contains(key)) { entityBytes = new byte[0]; isDelete = true; } else { TraceLog.WriteError("EntitySync queue key {0} faild object is null.", key); continue; } byte[] stateBytes = BufferUtils.GetBytes(isDelete ? 1 : 0); byte[] values = BufferUtils.MergeBytes(BufferUtils.GetBytes(idBytes.Length + stateBytes.Length), idBytes, stateBytes, entityBytes); pipeline.QueueCommand(c => ((RedisClient)c).HSet(hashId, keyBytes, values)); hasPost = true; } if (hasPost) { pipeline.Flush(); } } finally { pipeline.Dispose(); } } } catch (Exception ex) { TraceLog.WriteError("EntitySync queue {0}", ex); } finally { Interlocked.Exchange(ref _entityQueueRunning, 0); } } }