-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add cache module #62
Changes from all commits
e40ac40
ae8129f
92f5805
e87f652
1e03bd5
cb82ec5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need documentation |
| 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}; | ||
jonathonmcmurray marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| / 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; | ||
|
Comment on lines
+33
to
+35
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inconsistent indentation |
||
| / 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]; | ||
| } | ||
|
|
||
|
|
||
|
Comment on lines
+68
to
+69
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. excessive blank lines between functions, please reduce to one blank line between each function |
||
|
|
||
| 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; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this line is mixing spaces & tabs for indentation |
||
| // 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} | ||
|
|
||
| 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]); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this right? I think we should probably also export |
||
|
|
||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need unit tests |
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.
this should be removed, temp file from vim