Esempio n. 1
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);
        }
Esempio n. 2
0
        public RedisData.Response Set(LambdaRedisArg data)
        {
            var resp = new RedisData.Response()
            {
                status = RedisError.E_CHAOS.ToString(), k = data.k
            };

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

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

                // TTLセットする場合
                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);
        }
Esempio n. 3
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");

            var redisClient = new RedisClient(data.apiUrl, data.apiKey, ctx);

            if (env != null)
            {
                ctx.Log("Environment [env] : " + env);
            }
            else // ローカルテスト時に使う
            {
                ctx.Log("Environment [env] : null");
            }

            if (data.mode == "set")
            {
                ret = await redisClient.Set(data.k, data.v, data.ttlSec);
            }
            else if (data.mode == "get")
            {
                ret = await redisClient.Get(data.k);
            }
            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);
        }
Esempio n. 4
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);
        }
Esempio n. 5
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; // Null許容オプション付いてるので...
                        resp.ttlSec = (int)tmp.TotalSeconds;
                    }
                    resp.status = RedisError.E_OK.ToString();
                }
                //resp.v = cache.StringGet(data.k);
                //if(resp.v == null){ resp.v = ""; resp.status = RedisError.E_NOTFOUND.ToString(); }
                //else              {              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);
        }
Esempio n. 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);
        }