-
Notifications
You must be signed in to change notification settings - Fork 35
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
11 changed files
with
150 additions
and
2 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
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
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
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,39 @@ | ||
# @util | ||
|
||
The `@util` module provides various utilities. | ||
|
||
## API | ||
|
||
```py | ||
util = require('@util') | ||
``` | ||
|
||
### @util.memoize(ttl) | ||
|
||
A decorator that allows memoization of a function based on | ||
the arguments passed to it: | ||
|
||
```py | ||
@util.memoize(60) | ||
f expensive_task(x, y, z) { | ||
# do something very expensive here... | ||
} | ||
``` | ||
|
||
The first time `expensive_task` gets called with a set of arguments, | ||
it will be execute. The next time it's called with the same set of | ||
arguments its result will be fetched from a cache (currently | ||
implemented in-memory). Executions are going to be cached for a | ||
specific timeframe, `ttl` (in seconds). | ||
|
||
Arguments are serialized using the `str()` method: | ||
|
||
```bash | ||
[12, {}, 0.23, "hello"].str() # "[12, {}, 0.23, \"hello\"]" | ||
``` | ||
|
||
## Next | ||
|
||
That's about it for this section! | ||
|
||
You can now head over to read a little bit about [how to install 3rd party libraries](/misc/3pl). |
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
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
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
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
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
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,15 @@ | ||
memo = require('@util').memoize | ||
|
||
@memo(60) | ||
f long_task(x, y) { | ||
sleep(1000) | ||
return "done" | ||
} | ||
|
||
echo(long_task(1, 1)) | ||
echo(long_task(1, 1)) | ||
echo(long_task(1, 1)) | ||
echo(long_task(1, 1)) | ||
echo(long_task(1, 1)) | ||
echo(long_task(1, 1)) | ||
echo(long_task(1, 2)) |
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,38 @@ | ||
memo_map = {} | ||
|
||
# Decorator to memoize | ||
# the result of a function. | ||
f memoize(ttl) { | ||
# We want to store time to the millisecond, | ||
# allowing for TTLs to be < 1s (eg 0.250). | ||
ttl = ttl * 1000 | ||
|
||
return f(fn) { | ||
return f() { | ||
serialized_args = ....str() | ||
cached = memo_map[serialized_args] | ||
now = unix_ms() | ||
|
||
if cached { | ||
if cached.ts + ttl - now > 0 { | ||
return cached.result | ||
} | ||
|
||
memo_map.pop(serialized_args) | ||
} | ||
|
||
res = fn.call(...) | ||
|
||
memo_map[serialized_args] = { | ||
"ts": now, | ||
"result": res | ||
} | ||
|
||
return res | ||
} | ||
} | ||
} | ||
|
||
return { | ||
"memoize": memoize | ||
} |