We have updated Flexmonster Software License Agreement, effective as of September 30, 2024. Learn more about what’s changed.
All documentation
  • Introduction
  • Connecting to data source
    1. Supported data sources
    2. Connecting to other data sources
  • Browser compatibility
  • Documentation for older versions
  • Manually reloading indexes

    Out of the box, the Data Server can automatically reload indexes after any number of minutes. In this guide, we describe how to configure the Data Server as a DLL so it allows reloading indexes manually. First, we will implement a custom service for reloading indexes. Then, we will register the service in the application. We have also prepared an example of implementing and using the service.

    Note Knowledge of core C# concepts is required to fully understand this guide.

    Step 1. Implement a service for reloading indexes

    Step 1.1. Create a custom service (e.g., IndexReloadService) that will be responsible for reloading indexes:

    public class IndexReloadService
    {}

    Step 1.2. Add the IDataStorage field (e.g., _dataStorage) to the service:

    public class IndexReloadService
    {
       private readonly IDataStorage _dataStorage;
    
    
       public IndexReloadService(IDataStorage dataStorage)
       {
          _dataStorage = dataStorage;
       }
    }

    IDataStorage describes objects responsible for storing data for indexes:

    public interface IDataStorage
    {
       public Task<IDataStructure> GetOrAddAsync(string cacheKey);
       public void Remove(string key);
    }

    The GetOrAddAsync method gets the index's data from the server's RAM. If there is no data for the index in the RAM, the method reloads the data. The Remove method is used to remove the index's data. Combine these methods to reload indexes. IDataStorage is provided by the Data Server as a DLL.

    Step 1.3. Declare the Reload method that reloads an index using the Remove and the GetOrAddAsync methods:

    public class IndexReloadService
    {
       private readonly IDataStorage _dataStorage;
    
    
       public IndexReloadService(IDataStorage dataStorage)
       {
          _dataStorage = dataStorage;
       }
    
       public async Task Reload(string indexName)
       {
          // Reload the index
          _dataStorage.Remove(indexName);
          await _dataStorage.GetOrAddAsync(indexName);
       }
    }

    Step 2. Register the service

    Register the created service (e.g., IndexReloadService) in the Program.cs file:

    using Flexmonster.DataServer.Core;
    
    var builder = WebApplication.CreateBuilder(args);
    builder.Services.ConfigureFlexmonsterOptions(builder.Configuration);
    builder.Services.AddFlexmonsterApi();
    builder.Services.AddControllers();
    builder.Services.AddCors();
    builder.Services.AddScoped<IndexReloadService>();
    // Other configurations

    You can now use the service's Reload method to reload indexes manually. Check out our example server implementation, where the index is reloaded on a button click in a client-side application.

    Note If you want to reload indexes only manually, explicitly set the DataRefreshTime and RefreshTime properties to 0

    Example

    We prepared a sample project that demonstrates how the manual index reloading feature can be implemented.

    On the server side, we created an endpoint that reloads an index on an HTTP request. On the client side, we created a button that sends the HTTP request to the endpoint and updates data on a successful response.

    Reloading the index on the server

    In the sample application, the server reloads an index when the /refresh endpoint receives an HTTP request with the index's name. FlexmonsterAPIController.cs contains the endpoint definition:

    [Route("refresh")]
    [HttpGet]
    public async Task<IActionResult> RefreshIndex([FromQuery]string index)
    {
      await _reloadService.Reload(index);
      return new JsonResult("");
    }

    Notice the Reload method. This method is described in ReloadService.cs and is used to reload indexes.

    Triggering index reloading from the browser

    In the sample, a user can trigger index reloading by clicking the button on the main page. An event handler is added to the button using jQuery in site.js. The handler sends a request to the /refresh endpoint and updates the data for the report if the request succeeds:

    $("#reloadButton").on("click", function () {
      $.ajax({
        url: "http://localhost:51718/api/refresh?index=" + report.dataSource.index,
        type: "get",
        success: function () {
          pivot.updateData(report.dataSource);
      }
    });

    What's next?

    You may be interested in the following articles: