-
Notifications
You must be signed in to change notification settings - Fork 1
Add cache module #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jonathonmcmurray
wants to merge
6
commits into
main
Choose a base branch
from
cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add cache module #62
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e40ac40
cache initial commit
jcliffDI ae8129f
placed module in di directory
j-muldoon 92f5805
Added ; to line ends
ryanCheale e87f652
- Removed instances of .z.m
ryanCheale 1e03bd5
removal of 0N\!, tidied up init.q
ryanCheale cb82ec5
Complete cache code transfer, runs but local namespacing for variable…
ryanCheale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Empty file.
This file contains hidden or 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,95 @@ | ||
| / Library to provide a mechanism for storing function results in a cache and returning them from the cache if they are available and non stale. | ||
|
|
||
| / return timestamp function | ||
| cp:{.z.p}; | ||
|
|
||
| / the maximum size of the cache in MB | ||
| maxsize:10; | ||
|
|
||
| / the maximum size of any individual result set in MB | ||
| maxindividual:50; | ||
|
|
||
| / make sure the maxindividual isn't bigger than maxsize | ||
| maxindividual:maxsize&maxindividual; | ||
|
|
||
| MB:2 xexp 20; | ||
|
|
||
| / a table to store the cache values in memory | ||
| cache:([id:`u#`long$()] lastrun:`timestamp$();lastaccess:`timestamp$();size:`long$()); | ||
|
|
||
| / a dictionary of the functions | ||
| funcs:(`u#`long$())!(); | ||
| / the results of the functions | ||
| results:(`u#`long$())!(); | ||
|
|
||
| / table to track the cache performance | ||
| perf:([]time:`timestamp$();id:`long$();status:`symbol$()); | ||
|
|
||
| id:0j; | ||
| getid:{:id+::1}; | ||
|
|
||
| / add to cache | ||
| add:{[function;id;status] | ||
| / Don't trap the error here - if it throws an error, we want it to be propagated out | ||
| res:value function; | ||
| $[(maxindividual*MB)>size:-22!res; | ||
| / check if we need more space to store this item | ||
| [now:cp[]; | ||
| if[0>requiredsize:(maxsize*MB) - size+sum exec size from cache; evict[neg requiredsize;now]]; | ||
| / Insert to the cache table | ||
| `cache upsert (id;now;now;size); | ||
| / and insert to the function and results dictionary | ||
| funcs[id]:enlist function; | ||
| results[id]:enlist res; | ||
| / Update the performance | ||
| trackperf[id;status;now]]; | ||
| / Otherwise just log it as an addfail - the result set is too big | ||
| trackperf[id;`fail;cp[]]]; | ||
| / Return the result | ||
| res}; | ||
|
|
||
| // Drop some ids from the cache | ||
| drop:{[ids] | ||
| ids,:(); | ||
| delete from `cache where id in ids; | ||
| `results : ids _ `results; | ||
| } | ||
|
|
||
| // evict some items from the cache - need to clear enough space for the new item | ||
| // evict the least recently accessed items which make up the total size | ||
| // feel free to write a more intelligent cache eviction policy ! | ||
| evict:{[reqsize;currenttime] | ||
| r:select from | ||
| (update totalsize:sums size from `lastaccess xasc select lastaccess,id,size from cache) | ||
| where prev[totalsize]<reqsize; | ||
| drop[r`id]; | ||
| trackperf[r`id;`evict;currenttime]; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| trackperf:{[id;status;currenttime] `perf insert ((count id)#currenttime;id;(count id)#status)}; | ||
|
|
||
|
|
||
| // check the cache to see if a function exists with a young enough result set | ||
| execute:{[func;age] | ||
| // check for a value in the cache which we can use | ||
| $[count r:select id,lastrun from .cache.cache where .cache.funcs[id]~\:enlist func; | ||
| // There is a value in the cache. | ||
| [r:first r; | ||
| // We need to check the age - if the specified age is greater than the actual age, return it | ||
| // else delete it | ||
| $[age > (now:.proc.cp[]) - r`lastrun; | ||
| // update the cache stats, return the cached result | ||
| [update lastaccess:now from `.cache.cache where id=r`id; | ||
| trackperf[r`id;`hit;now]; | ||
| first results[r`id]]; | ||
| // value found, but too old - re-run it under the same id | ||
| [drop[r`id]; | ||
| add[func;r`id;`rerun]]]]; | ||
| // it's not in the cache, so add it | ||
| add[func;getid[];`add]]} | ||
|
|
||
| // get the cache performance | ||
| getperf:{update function:.cache.funcs[id] from .cache.perf} | ||
|
|
||
This file contains hidden or 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,5 @@ | ||
| / Load core functionality into root module namespace | ||
| \l ::cache.q | ||
|
|
||
| export:([add]); | ||
|
|
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is this doing? its not used anywhere else in this script.. i guess this begs the question is the id incrementation working as expected?