Caching data inside PowerGUI

Warning: This is a pretty technical how-to post for those extending PowerGUI.🙂

Now to the topic. PowerGUI is extremely honest. All it does to the system is just running PowerShell commands and giving you the outcome (and the code). This is great, but sometimes it can also be slow.

The upcoming 1.0.12 release is improving the performance quite a bit by re-using the collection of objects it already retrieved when you click on any of the headers to do a sort, or when you filter the data, or when you click any action or link. So for example, if you click Processes and then do something with the processes you retrieved, PowerGUI will not call Get-Process again, but will do sort, filter or some kind of pipelining on the collection you’ve already got.

The one situation in which this does not help is getting back to a node after you navigated away from it. Each time you click a node, PowerGUI is re-retrieving the data. There are situations in which you don’t need this (when the data is not that dynamic) so in this post I will show how you can make PowerGUI cache the data instead.

The example below is the code for a sample script node which I created to retrieve local system processes only the first time you click it and then on each subsequent click just use the previously cached collection:

# Cached Processes node to demo caching techniques in PowerGUI 1.0.11-12
# Try loading previously cached data
$ProcessesCache=[System.AppDomain]::CurrentDomain.GetData("ProcessesCache");

# If no cache available query the servers
if ($ProcessesCache -eq $null) {
    $ProcessesCache = Get-Process
# Store the collection in cache
[AppDomain]::CurrentDomain.SetData("ProcessesCache", $ProcessesCache);
}

# Display results
$ProcessesCache

The code above will try to load a previously cached collection and only make the actual system code if the collection is not available in cache (-eq $null). This will make PowerGUI only make the call the first time you click the node after starting the tool.

Depending on how static your data actually is you might want to add some Refresh logic into your links, actions, etc.

In the future releases we will probably make caching even easier but this should let you start using it today without waiting for us (like folks like Antonio are apparently already doing)

Tags: , , , ,

0 Responses to “Caching data inside PowerGUI”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




My Recent Tweets

Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

November 2007
M T W T F S S
« Oct   Dec »
 1234
567891011
12131415161718
19202122232425
2627282930  

%d bloggers like this: