-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
def db [] { | ||
[$env.XDG_CACHE_HOME "nu-complete.sqlite"] | path join | ||
} | ||
|
||
def init [] { | ||
if (db | path exists) { | ||
stor import --file-name (db) | ||
} else { | ||
(stor create | ||
--table-name "nu_complete" | ||
--columns { | ||
name: str | ||
value: str | ||
timestamp: datetime | ||
} | ||
) | ||
} | ||
} | ||
|
||
export def "nu-complete cache" [ | ||
--expire: duration | ||
cmd: any | ||
] { | ||
init | ||
|
||
let name = view source $cmd | ||
|
||
let saved = stor open | ||
| (query db | ||
"SELECT value, timestamp FROM nu_complete WHERE name = :name" | ||
-p { name: $name }) | ||
| get -i 0 | ||
|
||
if $saved == null or (($saved.timestamp | into datetime) + $expire < (date now)) { | ||
let output = do $cmd | ||
|
||
if $saved == null { | ||
{ name: $name, value: ($output | to nuon -r), timestamp: (date now) } | ||
| stor insert -t "nu_complete" | ||
} else { | ||
stor open | ||
| query db "UPDATE nu_complete SET value = :value, timestamp = :timestamp WHERE name = :name" -p { | ||
value: ($output | to nuon -r), | ||
timestamp: (date now), | ||
name: $name | ||
} | ||
} | ||
|
||
rm -f (db) | ||
stor export --file-name (db) | ||
|
||
$output | ||
} else { | ||
rm -f (db) | ||
stor export --file-name (db) | ||
|
||
$saved.value | from nuon | ||
} | ||
} | ||
|