KeyValStore.ahk - Convenient data persistence

Post your working scripts, libraries and tools for AHK v1.1 and older
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

KeyValStore.ahk - Convenient data persistence

11 Apr 2016, 13:18

KeyValStore.ahk

Key-value store for convenient local data persistence for config, caching, etc.. Heavily inspired by configstore

Data is stored in XML format.


Usage:

Code: Select all

#Include <KeyValStore> ; or call function KeyValStore() directly to auto-#Include
    
dat := new KeyValStore("test.xml") ; assumed in %A_WorkingDir%

dat.Set("foo", "Some value")

MsgBox % dat.Get("foo") ; displays 'Some value'

; use dot notation to access nested properties
; escape literal dot with '\' e.g.: 'foo.bar\.baz'
dat.Set("bar.baz", "Another value")
MsgBox % dat.Get("bar.baz") ; 'Another value'

bar := dat.Get("bar") ; {baz: "Another value"}

; get entire contents
all := dat[] ; {foo: "Some value", bar: {baz: "Another value"}}
; set new contents
dat[] := {hello: "Hello World", ahk: "AutoHotkey"}

dat.Del("ahk") ; delete an item, returns the removed item

dat.Clear() ; delete all items

MsgBox % dat.Path ; full path to file
Set(), Del() and Clear() will automatically sync the changes into the file. Releasing the instance does this as well. Tip: To bypass the autosave feature - fetch the desired nested property(using Get()) or root property(using instance[]), perform all the necessary operation, then assign it back when you're ready to sync.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: KeyValStore.ahk - Convenient data persistence

12 Apr 2016, 00:43

Thx for this. Can be usefull.
After dat.Clear() there is still <object/> + \r\n in test.xml.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: KeyValStore.ahk - Convenient data persistence

13 May 2016, 21:49

jNizM wrote:After dat.Clear() there is still <object/> + \r\n in test.xml.
Clearing the XML file invalidates it. Calling load(XmlFile) on an XML file w/o a root element would trigger a parse error. I think leaving {}(in XML <object/>) as default for an empty store should be the way it is.
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: KeyValStore.ahk - Convenient data persistence

19 Jul 2016, 23:24

This is a great way to store object-based program settings without implementing the save/restore functions... very convenient.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 163 guests