Ocelot icon indicating copy to clipboard operation
Ocelot copied to clipboard

Do Caching working on my setup ?

Open gfelot opened this issue 6 years ago • 5 comments

I have followed this part of the doc literally : https://ocelot.readthedocs.io/en/latest/features/caching.html

Then if I spam a request my first tough was to get a log like this : ConnectionId:**OcRequestId** => RequestId:**OcRequestId**:00000001 RequestPath:/api/stats/collected

and get a response from this cached request for the number of time I set it up.

Instead I got something like : Ocelot.Errors.Middleware.ExceptionHandlerMiddleware[0] => ConnectionId:0HLHK4OD3CE5E => RequestId:0HLHK4OD3CE5E:**00000004** RequestPath:/api/stats/collected

with the numbers that add up everytime... and the RequestIdKey is random.

That also make me see that the limit rate that I setup doesn't work too... Does the GlobalConfiguration part of my conf is working ?

ocelot.json

{
  "GlobalConfiguration": {
    "BaseUrl": "http://0.0.0.0:5000",
    "RequestIdKey": "OcRequestId",
    "RateLimitOptions": {
      "ClientWhitelist": [],
      "EnableRateLimiting": true,
      "Period": "30s",
      "PeriodTimespan": 1,
      "Limit": 2
    },
    "AdministrationPath": "/administration"
  },
  "ReRoutes": [
    {
      "UpstreamPathTemplate": "/api/stats/collected",
      "UpstreamHttpMethod": [
        "Get"
      ],
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "blabla.azurewebsites.net",
          "Port": 443
        }
      ],
      "DownstreamPathTemplate": "/v4/home/get",
      "FileCacheOptions": { "TtlSeconds": 600, "Region": "stats" }
    }
  ]
}

Startup.json

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Ocelot.Administration;
using Ocelot.Cache.CacheManager;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

namespace APIGateway.Base
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services
                .AddOcelot(Configuration)
                .AddCacheManager(c => { c.WithDictionaryHandle(); })
                .AddAdministration("/administration", "XXXXXXXXX");
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            //console logging  
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseOcelot().Wait();
            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }
}

Did I miss something ?

gfelot avatar Oct 17 '18 11:10 gfelot

@gfelot I think the global configuration rate limiting stuff only applies if you are using dynamic routing. I think you need to set it per reroute.

I am confused by your issue, what is actually not working? Can you paste a full log?

TomPallister avatar Oct 21 '18 17:10 TomPallister

Yes I put the caching configuration for each route and it's working now. But it cache the response only by the checking the url. On a POST if I modify the JSON body, Ocelot send me the last response.

gfelot avatar Oct 24 '18 14:10 gfelot

That last part looks like a bug to me, converting this issue.

philproctor avatar Jan 10 '19 00:01 philproctor

Yes I put the caching configuration for each route and it's working now. But it cache the response only by the checking the url. On a POST if I modify the JSON body, Ocelot send me the last response.

Do caching working in your setup ? if yes , pls help me i am not able flush the cache , please see this https://github.com/ThreeMammals/Ocelot/issues/1212

Please help ?

rajeshgithub001 avatar May 19 '20 11:05 rajeshgithub001

Is the caching outputted to the console when you say it's working?

AizenSousuke avatar Jun 21 '22 08:06 AizenSousuke

Implemented and fixed by #889

raman-m avatar Nov 28 '23 10:11 raman-m