Clear System Cache on one server from a web page on another server (C#) in Kentico

Woodley Westbrook asked on October 18, 2018 21:04

Hello Kentico DevNet,

I do not know if this is possible but thought I would submit the question anyway.

We have a Stage environment and Production environment up and running using Kentico 10.

We would like to give non Administrative personnel the ability to "Clear System Cache" in the Production environment by clicking a button on a C# web page located within Stage environment.

We constructed a way to authenticate the users to the page in Stage. This way we ensure that only authorized individuals have access.

We do not want them to have access to the "System" portion of the application in either environments so that we may limit the harm they could possibly do to the Stage or Production.

The part that we are having difficulty with is making a "secure request" to a page in Prod that will initiate the Clear Cache process.

Michal Samuhel greatly assisted us in constructing a C# page that does indeed clear the cache within that same environment.

Now we would like to see if we can take it one step further by doing what we described above. Clearing the cache in one environment from another environment.

We are research this https://odetocode.com/Articles/162.aspx which basically sends a Webclient to pass some credentials but were not sure if we are on the right path.

Any suggestions or examples would be appreciated.

We are using Kentico 10.

Thanks

Woodley Westbrook

Correct Answer

Dev User answered on October 18, 2018 21:12

I think you're on the right path. What I'd suggest something on a button click on staging and a scheduled task on production.

When they click the button in staging, it calls a service/page on the production site, which writes a record to a table. Then have a custom scheduled task run every X minutes to process that table. There should never be more than 1 record in the table and it could simply have 3 fields in it, RequestSentDate, RequestType, RequestProcessedDate. This way you leave the processing directly on the production server and don't have a dropped call with a web request if the cache or server instance is restarted.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Peter Mogilnitski answered on October 18, 2018 22:00 (last edited on October 18, 2018 22:00)

Technically you have to examen what happens inside: CMS\CMSModules\System\Controls\System.ascx.cs. There is a piece that does exactly what you need:

    // Clear the cache
    CacheHelper.ClearCache(null, true);
    Functions.ClearHashtables();

    // Drop the routes
    CMSDocumentRouteHelper.DropAllRoutes();

    // Disposes all zip files
    ZipStorageProvider.DisposeAll();

    // Collect the memory
    GC.Collect();
    GC.WaitForPendingFinalizers();

    // Log event
    EventLogProvider.LogEvent(EventType.INFORMATION, "System", "CLEARCACHE", GetString("Administration-System.ClearCacheSuccess"));

    ShowConfirmation(GetString("Administration-System.ClearCacheSuccess"));

The problem that you have to run this as Administrator and you have to put a lot of restrictions and checks for example:

  1. 1 restart for 30 mins
  2. access from only internal/specific IPs etc...
0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on October 19, 2018 09:10

Hi,

Taking one step back, the "Clear Cache" mechanism is quite a drastic measure. What would be the use case for a non-admin to basically reset the whole application? What are you hoping to remove from cache?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.