# Caching data between script runs?

**URL:** <https://forum.inductiveautomation.com/t/caching-data-between-script-runs/57275>\
**Category:** General Discussion\
**Created:** [March 16, 2022, 7:08pm UTC](https://forum.inductiveautomation.com/t/caching-data-between-script-runs/57275 "2022-03-16T19:08:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://forum.inductiveautomation.com/u/peter)\
**Post date:** [March 16, 2022, 7:08pm UTC](https://forum.inductiveautomation.com/t/caching-data-between-script-runs/57275/1 "2022-03-16T19:08:17Z")

</div>

This is a purely hypothetical question, but as I can’t answer it myself it must mean that my Ignition knowledge is lacking.

Given a script that runs repetively, such as a timer script or a change valve script, is there a mechanism for caching data within the jython scripting space for a particular instance of the script? And can this be done without hiding the data in a tag somewhere?

I’m doing a mental exercise of lazy loading of resources and wondering if it can be done. And example of this would be constructing an array of paths that would be fed into a tag read function - and not doing it on the fly every time the script runs. (and yes I know that is potentially a micro-optimization - unless the number of instances starts to challenge the memory limitations of the GW)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 16, 2022, 7:17pm UTC](https://forum.inductiveautomation.com/t/caching-data-between-script-runs/57275/2 "2022-03-16T19:17:52Z")

</div>

I think this is about as close as you can get.

> [@system.util.getGlobals() not persisted across Designer Save](https://forum.inductiveautomation.com/t/system-util-getglobals-not-persisted-across-designer-save/33869/14):
>
> Ok. New module “Life Cycle” w/ system.util.persistent(name) utility function. For v7.9: [https://www.automation-pros.com/ignition/Life%20Cycle-0.9.0.2004121825.modl](https://www.automation-pros.com/ignition/Life%20Cycle-0.9.0.2004121825.modl) For v8.0: [https://www.automation-pros.com/ignition/Life%20Cycle-0.9.0.2004121920-v8.modl](https://www.automation-pros.com/ignition/Life%20Cycle-0.9.0.2004121920-v8.modl) Test with a project script like this: # project script "Life" g = system.util.persistent('LifeTest') count = g.setdefault('editCount', -1) count += 1 g['editCount'] = count def editCount(): return count Print the return from Life.editCou…

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 16, 2022, 7:20pm UTC](https://forum.inductiveautomation.com/t/caching-data-between-script-runs/57275/3 "2022-03-16T19:20:31Z")

</div>

Every variable you create at the outermost/topmost level persists until the script is restarted. Create a named dictionary outside of any function and any keys you set on it will persist. Also valuable for constants and one-time generated pseudo-constants like lists of tag paths.

If you need persistence that survives script restarts, follow the link @lrose posted. Many caching activities do not need that kind of persistence.

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://forum.inductiveautomation.com/u/peter)\
**Post date:** [March 16, 2022, 7:31pm UTC](https://forum.inductiveautomation.com/t/caching-data-between-script-runs/57275/4 "2022-03-16T19:31:05Z")

</div>

> [@pturmel](#):
>
> Every variable you create at the outermost/topmost level persists until the script is restarted.

What defines restart? That's a very vague concept. However as most of things I'm talking about are GW based scripts (timer and tag change) that nominally this would be a GW restart (or a script edit)

And no, I'm not looking for the persistence that @lrose is talking about, but it is interesting to know that exists.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 16, 2022, 7:42pm UTC](https://forum.inductiveautomation.com/t/caching-data-between-script-runs/57275/5 "2022-03-16T19:42:31Z")

</div>

> [@peter](#):
>
> What defines restart? That’s a very vague concept.

Not vague at all. Just a save a project in the designer and its gateway scripts will restart. (And if an inheritable project, any dependent projects' scripts will restart.) Look at your gateway log while you save.

Each project in the gateway has a jython script environment that includes all of the relevent script modules. After a restart, the first reference to any name in a script module causes that script module to be imported, establishing its namespace. The import happens under an interpreter lock to ensure it completes before any referenced name is actually used. That means top-level variables defined anywhere in the script are present before any function defined in the module can run. The only exception would be if the top level of the script itself calls one of its own functions.

Similarly, if a function called in a script module calls another script's function, and happens to be the first reference, that other script will be imported at that point.
