Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell command for removing items from Appfabric cache

Are there powershell commands to:

  1. Get a list of items in the cache
  2. Remove a particular item
  3. Remove all items
  4. Change the value for a particular key

I haven't come across a nice blog or tutorial for beginners to get started with Appfabric caching administration.

Thanks!

like image 415
DotnetDude Avatar asked Oct 14 '22 07:10

DotnetDude


1 Answers

Unfortunately not :-( Currently the PowerShell commands are aimed at a higher level of granularity.

However...

You can write your own PowerShell cmdlets, so you can add the extra ones you need :-)

There's plenty of information on the web about writing custom cmdlets but as a rough guide it'll be something like this. Build a new Class Library project in your language of choice. Add a reference to System.Management.Automation.dll - you can find it in C:\Program Files\Reference Assemblies\Microsoft\Powershell\1.0. Create a class that inherits from Cmdlet and also has the Cmdlet attribute. Override the ProcessRecord method and add the code to do what you need to do. To pass in parameters from Powershell you need to add properties to your class and mark them with the Parameter attribute. It ought to look something like this:

Imports System.Management.Automation 
Imports Microsoft.ApplicationServer.Caching

<Cmdlet(VerbsCommon.Remove, "CacheItem")> _
Public Class RemoveCacheItem
    Inherits Cmdlet

    Private mCacheName As String
    Private mItemKey As String

    <Parameter(Mandatory:=True, Position:=1)> _
    Public Property CacheName() As String
        Get
            Return mCacheName
        End Get
        Set(ByVal value As String)
            mCacheName = value
        End Set
    End Property

    <Parameter(Mandatory:=True, Position:=2)> _
    Public Property ItemKey() As String
        Get
            Return mItemKey
        End Get
        Set(ByVal value As String)
            mItemKey = value
        End Set
    End Property

    Protected Overrides Sub ProcessRecord()

        MyBase.ProcessRecord()

        Dim factory As DataCacheFactory
        Dim cache As DataCache

        Try
            factory = New DataCacheFactory

            cache = factory.GetCache(Me.CacheName)

            Call cache.Remove(Me.ItemKey)
        Catch ex As Exception
            Throw
        Finally
            cache = Nothing
            factory = Nothing
        End Try

    End Sub

End Class

Once you've built the DLL, you can add it into Powershell with the Import-Module cmdlet.

like image 92
PhilPursglove Avatar answered Oct 19 '22 03:10

PhilPursglove