You are here: Core Reference > Coding > Logic > Using VAR NOCACHE to Prevent Cached Values Being Used in an ST Program

Using VAR NOCACHE to Prevent Cached Values Being Used in an ST Program

You can use the NOCACHE keyword in your Logic programs to prevent the program from using a cached value. This is required when you need a value to accumulate each time a program executes, for example, when a total count value needs to increase when an input value changes.

If the NOCACHE keyword is not used, the ST program will use the value that was in place when the program first executed. This means that when a series of values are reported for one of the ST program's inputs, the value of the variable that is affected by the inputs will be the same. In the example below, the NOCACHE keyword is used to prevent the program from calculating the Total value as 0.0 + the current value of the Value variable. So, if the program receives 10 values for the Value variable, the program will use the default value of 0.0 for the Total value on the first execution, but will not use 0.0 on subsequent executions. If the NOCACHE keyword was not used, the program would use the 0.0 value for the Total value for each execution.

Example:

  • PROGRAM TotalValues
    • VAR
      • FirstTime:BOOL := TRUE;
    • END_VAR
    • VAR
      • Value AT %M(Input Point Value): REAL;
    • END_VAR
    • VAR NOCACHE
      • Total AT %M(Total Point): REAL;
    • END_VAR;
    • IF FirstTime THEN
      • Total := 0.0;
      • FirstTime := FALSE;
    • END_IF;
    • Total := Total+Value;
  • END_PROGRAM

In this example, the FirstTime value is a Boolean value (true/false) that is used to define whether the program is being executed for the first time. By default, it is set to TRUE.

The Value variable is a real value that the program reads from the Input Point Value database item.

The Total value is a NOCACHE value (it is not cached and is retrieved each time the program executes). The Total value is a real value that is calculated by the program each time it executes. The result of the calculation is written to the Total Point database item.

The If statement determines whether the Total value is 0 or is another value. If the FirstTime value is TRUE then the Total value is 0.0 (the FirstTime value is only TRUE the first time that the program executes). If the FirstTime value is FALSE, the FALSE value is maintained and the Total value is the current Total value + the value of the Value variable.

This means that the first time the program executes, the Total value is 0.0. Before the first execution of the program completes, it sets the FirstTime value to FALSE so that the next execution of the program will cause the Total value to change from 0.0. At the end of the first execution of the program, the Total value is set to the same value as the Value variable (Total=0.0 + Value = Same value as Value variable).

On subsequent executions of the program, the FirstTime value will be FALSE and so the Total will be the current Total value + the value of the Value variable. The program will not use 0.0 as the Total value each time, as the 0.0 is not cached.

To define a NOCACHE value, you have to create a separate VAR list for the values to which the NOCACHE feature applies. The VAR list has to be started as VAR NOCACHE. The values need to be entered beneath the VAR NOCACHE definition in the same way as with normal VAR lists, and the VAR list needs to be closed with END_VAR.


ClearSCADA 2015 R2