Beispiel #1
0
        public RedisData.Response Get(LambdaRedisArg data)
        {
            var resp = new RedisData.Response()
            {
                status = RedisError.E_CHAOS.ToString(), k = data.k
            };

            try{
                ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(data.redisURI);
                IDatabase             cache = redis.GetDatabase();
                var redisResp = cache.StringGetWithExpiry(data.k);
                if (redisResp.Value.IsNullOrEmpty)
                {
                    resp.v = ""; resp.status = RedisError.E_NOTFOUND.ToString();
                }
                else
                {
                    resp.v = redisResp.Value.ToString();
                    if (redisResp.Expiry != null)
                    {
                        TimeSpan tmp = (TimeSpan)redisResp.Expiry;
                        resp.ttlSec = (int)tmp.TotalSeconds;
                    }
                    resp.status = RedisError.E_OK.ToString();
                }
            }
            catch (Exception ex) {
                m_ctx.Log(ex.Message);
                resp.status = RedisError.E_CRITICAL.ToString();
                resp.meta   = ex.Message;
            }
            return(resp);
        }
Beispiel #2
0
        async Task <RedisData.Response> ClientJob(LambdaRedisArg data, ILambdaContext ctx)
        {
            RedisData.Response ret = null;

            var env     = System.Environment.GetEnvironmentVariable("LAMBDA_ENV");
            var service = System.Environment.GetEnvironmentVariable("LAMBDA_SERVICE");

            data.apiUrl = System.Environment.GetEnvironmentVariable("DEBUG_API_URL");
            data.apiKey = System.Environment.GetEnvironmentVariable("DEBUG_API_KEY");

            if (env != null)
            {
                ctx.Log("Environment [env] : " + env);
            }
            else
            {
                ctx.Log("Environment [env] : null");
            }

            if (data.mode == "set")
            {
                var checkRet = await RedisClientHelper.Set(ctx, data.apiUrl, data.apiKey, data.k, data.v, data.ttlSec);

                ret = new RedisData.Response()
                {
                    status = checkRet.ToString(), k = data.k, v = data.v
                };
            }
            else if (data.mode == "get")
            {
                data.v = await RedisClientHelper.Get(ctx, data.apiUrl, data.apiKey, data.k);

                ret = new RedisData.Response()
                {
                    status = RedisError.E_OK.ToString(), k = data.k, v = data.v
                };
            }
            else if (data.mode == "checkEqual")
            {
                var checkRet = await RedisClientHelper.CheckValueEqual(ctx, data.apiUrl, data.apiKey, data.k, data.v);

                ret = new RedisData.Response()
                {
                    status = checkRet.ToString(), k = data.k, v = data.v
                };
            }
            else
            {
                ret = await Task.Run(() => { return(new RedisData.Response()
                    {
                        status = RedisError.E_CHAOS.ToString()
                    }); });
            }

            ctx.Log("Ret : " + ret.status + " > [ " + ret.k + " : " + ret.v + " ]");

            return(ret);
        }
Beispiel #3
0
        public async Task <RedisData.Response> Set(string k, string v, int ttlSec = -1)
        {
            var resp = new RedisData.Response()
            {
                status = RedisError.E_CHAOS.ToString()
            };

            using (var client = new HttpClient()){
                try {
                    var request = new HttpRequestMessage(HttpMethod.Post, m_apiUrl);
                    request.Content = new StringContent
                                      (
                        JsonSerializer.ToJsonString(new RedisData.Request()
                    {
                        role   = "server",
                        mode   = "set",
                        k      = k,
                        v      = v,
                        ttlSec = ttlSec
                    })
                        , Encoding.UTF8, @"application/json"
                                      );

                    request.Headers.Add(@"x-api-key", m_apiKey);

                    var response = await client.SendAsync(request);

                    var responseContent = await response.Content.ReadAsStringAsync();

                    if (!String.IsNullOrEmpty(responseContent))
                    {
                        Log("Got Resp >> " + responseContent);
                        resp = JsonSerializer.Deserialize <RedisData.Response>(responseContent);
                    }
                    else
                    {
                        resp.status = RedisError.E_CRITICAL.ToString();
                    }
                }
                catch (Exception e) {
                    Log("Exception : " + e.ToString());
                    resp.status = RedisError.E_CRITICAL.ToString();
                }
            }
            return(resp);
        }
Beispiel #4
0
        public RedisData.Response Set(LambdaRedisArg data)
        {
            var resp = new RedisData.Response()
            {
                status = RedisError.E_CHAOS.ToString(), k = data.k
            };

            try {
                ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(data.redisURI);
                IDatabase             cache = redis.GetDatabase();

                m_ctx.Log("SetTTL > " + data.ttlSec.ToString());

                if (data.ttlSec > 0)
                {
                    if (cache.StringSet(data.k, data.v, TimeSpan.FromSeconds(data.ttlSec)))
                    {
                        resp.status = RedisError.E_OK.ToString();
                    }
                    else
                    {
                        resp.status = RedisError.E_CRITICAL.ToString();
                    }
                }
                else
                {
                    if (cache.StringSet(data.k, data.v))
                    {
                        resp.status = RedisError.E_OK.ToString();
                    }
                    else
                    {
                        resp.status = RedisError.E_CRITICAL.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                m_ctx.Log(ex.ToString());
                resp.status = RedisError.E_CRITICAL.ToString();
                resp.meta   = ex.Message;
            }
            return(resp);
        }
Beispiel #5
0
        async Task <RedisData.Response> ServerJob(LambdaRedisArg data, ILambdaContext ctx)
        {
            RedisData.Response ret = null;

            var redisServer = new RedisServer(ctx);

            var env     = System.Environment.GetEnvironmentVariable("LAMBDA_ENV");
            var service = System.Environment.GetEnvironmentVariable("LAMBDA_SERVICE");

            data.redisURI = System.Environment.GetEnvironmentVariable("REDIS_URI");

            if (env != null)
            {
                ctx.Log("Environment [env] : " + env);
            }
            else
            {
                ctx.Log("Environment [env] : null");
            }

            if (data.mode == "set")
            {
                ret = redisServer.Set(data);
            }
            else if (data.mode == "get")
            {
                ret = redisServer.Get(data);
            }
            else
            {
                ret = await Task.Run(() => { return(new RedisData.Response()
                    {
                        status = RedisError.E_CHAOS.ToString()
                    }); });
            }

            return(ret);
        }
Beispiel #6
0
        public override string Handler(LambdaRedisArg data, ILambdaContext context)
        {
            m_ctx  = context;
            m_data = data;

            m_data.sw.Start();

            m_ctx.Log(String.Format("{0:D8} : Start Process", m_data.sw.ElapsedMilliseconds));

            string resp;

            if (data.role == "client")
            {
                Task <RedisData.Response> result = ClientJob(data, context);
                m_ctx.Log(String.Format("{0:D8} : Wait Async Method", m_data.sw.ElapsedMilliseconds));
                resp = JsonSerializer.ToJsonString(result.Result);
            }
            else if (data.role == "server")
            {
                Task <RedisData.Response> result = ServerJob(data, context);
                m_ctx.Log(String.Format("{0:D8} : Wait Async Method", m_data.sw.ElapsedMilliseconds));
                resp = JsonSerializer.ToJsonString(result.Result);
            }
            else
            {
                var dummyResp = new RedisData.Response()
                {
                    status = RedisError.E_CHAOS.ToString()
                };
                resp = JsonSerializer.ToJsonString(dummyResp);
            }

            m_ctx.Log(String.Format("{0:D8} : Complete Process", m_data.sw.ElapsedMilliseconds));

            return(resp);
        }