Working with the Rule Application Cache

  • Updated

This article includes the sections:

Adding Items into the Cache

Prerequisites None
Namespaces InRule.Runtime
Classes FileSystemRuleApplicationReference, CatalogRuleApplicationReference,
InMemoryRuleApplicationReference, RuleSession
See Also Creating a RuleSession with Cache Retention, Controlling Compilation and Cache Retention, Iterating items in the cache 
References InRule.Runtime.dll, InRule.Common.dll

In general, items are automatically added to the cache when needed or explicitly using Compile; however, you can manually add rule applications into the cache:

string ruleAppPath = @"c:\temp\Invoice.ruleApp";
// add an item into the cache using a rule application reference
var ruleApp = new FileSystemRuleApplicationReference(ruleAppPath);
RuleSession.RuleApplicationCache.Add(ruleApp);

// add an item into the cache using the path, notice that the same options provided
// by the compile method are also available
RuleSession.RuleApplicationCache.Add(ruleAppPath, true, CacheRetention.FromWeight(3000));

Managing the Cache

Prerequisites A valid RuleApplicationReference
Namespaces InRule.Runtime
Classes RuleSession, RuleApplicationReference, FileSystemRuleApplicationReference,
InMemoryRuleApplicationReference, CatalogRuleApplicationReference
See Also Retrieving a Rule Application, Creating a RuleSession, Creating a RuleSession with Cache Retention, Adding items into the Cache, Iterating items in the cache, Controlling Compilation and Cache Retention

Compiling a rule application with delegates

When you compile your rule application, you can specify if executable code (delegates) will be compiled at the same time or only when needed. If you select the former, there will be a slower compile time, but the execution time will be faster.

// Compile the RuleApplicationReference, including the executable code (delegates)
ruleAppRef.Compile(CompileSettings.Default);

Setting the degree of parallelism for compilation

You can direct InRule regarding how many threads or cores to use for parallel work when compiling a rule application. This value is a non-negative integer, which you set as follows:

Value

Meaning

0

on; InRule will decide based on the number of cores available

1

off; one thread will be used for all work (default)

n

the number of threads to use

/ Compile the RuleApplicationReference, specifying at most two threads or cores
ruleAppRef.Compile(CompileSettings.Create(EngineLogOptions.None, 2));

InRule recommends the default setting (0).


Adding a rule application to the cache

After compiling your rule application, you can speed up future executions by adding it to the rule session cache.

// Add the rule application to the rule application cache
RuleSession.RuleApplicationCache.Add(ruleAppRef);

Removing a rule application from the cache

You may also remove a rule application from the cache.

// Remove a rule application from the rule application cache
RuleSession.RuleApplicationCache.Remove(ruleAppRef);

Clearing the rule application caches

From time to time, you may want to clear the caches completely. You may do this for the internal cache of a rule application or the entire rule session cache.

// Clear internal caches in a rule application reference
ruleAppRef.ClearCompiledFunctions();

// Clear the entire rule application cache
RuleSession.RuleApplicationCache.Clear();

Controlling Compilation and Cache Retention

Prerequisites None
Namespaces InRule.Runtime
Classes RuleApplicationReference
See Also Creating a RuleSession with Cache Retention, Adding items into the Cache, Iterating items in the cache 
References InRule.Runtime.dll, InRule.Common.dll
// You can check to see if a rule application is compiled
if (!ruleApp.IsCompiled)
{
  // Compile functions as well as metadata, if false only the
  // metadata is compiled
  bool compileFunctions = true;

  // Set the cache retention, AlwaysRetain will extend the cache to fit all
  // apps compiled with AlwaysRetain
   CacheRetention cacheRetention = CacheRetention.AlwaysRetain;

  // You can also use a weight to control how an application is retained in the
  // cache, a high weight take preference over lower weights - int.MaxValue is the
  // same as Always Retain
   cacheRetention = CacheRetention.FromWeight(2000);

  // Compile the rule app
   ruleApp.Compile(compileFunctions, cacheRetention);
}

Iterating Items in the Cache

Prerequisites None
Namespaces  InRule.Runtime
Classes RuleApplicationReference
See Also Creating a RuleSession with Cache Retention, Controlling Compilation and Cache Retention, Adding items into the Cache,
References InRule.Runtime.dll, InRule.Common.dll

You can iterate through all the cache entries to get information:

public string GetCacheEntriesInfo()
{
    var sb = new StringBuilder();
    // Get the existing cache entries from the RuleSession - this returns
    // a set of RuleApplicationReferences
    IEnumerable<RuleApplicationReference> cacheEntries;
    cacheEntries = RuleSession.RuleApplicationCache.Items;
    foreach (RuleApplicationReference cacheEntry in cacheEntries)
    {
        // Extract information from the rule application
        sb.AppendLine("Rule App Name:" + cacheEntry.GetRuleApplicationDef().Name);
        sb.AppendLine("Unique Name:" + cacheEntry.Name);

        // if it has been compiled, you can get properties such as time
        if (cacheEntry.LastMetadataCompile != null)
        {
            DateTime lastMetaCompileTime = cacheEntry.LastMetadataCompile.Value.UtcDateTime;
            sb.AppendLine("Last metadata compile:" + lastMetaCompileTime.ToString());
        }
        // get information about its rank in the cache
        sb.AppendLine("Cache retention rank:" + cacheEntry.CacheRetention.Weight.ToString());
        sb.AppendLine();
    }
    return sb.ToString();
}

Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.