-
Notifications
You must be signed in to change notification settings - Fork 23
Add ability to remove keys by type or API call #85
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,38 @@ The port the server should listen on. | |
|
||
The directory to use for temporary files. | ||
|
||
#### `ENABLE_TYPED_KEY_PREFIX_REMOVAL` | ||
|
||
- Default: `false` | ||
|
||
If enabled then any specifically structured keys will be automatically removed if there are more keys of such type than | ||
the amount specified via `MAX_STORED_KEYS_PER_TYPE`. | ||
Key type is defined by the following format: | ||
`{key_type}{TYPED_KEY_DELIMITER}{optional custom string}` | ||
|
||
Usually, it's recommended to use the following key structure: | ||
`{repository_name}_{branch}_{unique_key_name}{TYPED_KEY_DELIMITER}{hash or other unique data}` | ||
|
||
An example could look like: | ||
`falcondev-oss/github-actions-cache-server_dev_docker-cache@#@16ee33c5f9f59c0` | ||
|
||
In such case older keys with prefix `falcondev-oss/github-actions-cache-server_dev_docker-cache` will be automatically | ||
removed whenever there are more records for this key type then the one specified via `MAX_STORED_KEYS_PER_TYPE`. | ||
|
||
#### `MAX_STORED_KEYS_PER_TYPE` | ||
|
||
- Default: `3` | ||
|
||
If `ENABLE_TYPED_KEY_PREFIX_REMOVAL` is `true` then this is the maximum amount of the most recent keys to keep per key type. | ||
Any other older keys will be automatically removed. | ||
|
||
#### `TYPED_KEY_DELIMITER` | ||
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 believe cache keys cannot contain |
||
|
||
- Default: `@#@` | ||
|
||
If `ENABLE_TYPED_KEY_PREFIX_REMOVAL` is `true` then this is the delimiter which is used to identify the key type. | ||
Any prefix before `@#@` is considered to be the key type. | ||
|
||
## 2. Setup with Self-Hosted Runners | ||
|
||
Set the `ACTIONS_RESULTS_URL` on your runner to the API URL (with a trailing slash). | ||
|
@@ -148,3 +180,22 @@ variant: subtle | |
There is no need to change any of your workflows! 🔥 | ||
|
||
If you've set up your self-hosted runners correctly, they will automatically use the cache server for caching. | ||
|
||
## 4. Cache cleanup | ||
|
||
Cache cleanup can be triggered automatically via configured eviction policies like: | ||
- `CLEANUP_OLDER_THAN_DAYS` - eviction by time. | ||
- `ENABLE_TYPED_KEY_PREFIX_REMOVAL` - eviction by key type (key prefix). | ||
|
||
Moreover, an additional API exists which can be used to evict key records by using specified key prefix. | ||
API path: `/extra/clear_key_prefix`. API method: `POST`. | ||
Request body: `{ keyPrefix: string }`. | ||
|
||
Any keys with the prefix of `keyPrefix` will be removed and data will be cleared for such keys. | ||
Usually, it's useful to call keys removal for an archived branch or a closed PR where you know that the cache won't | ||
be reused anymore. For such cases you could have a step which is called and clears all keys prefixed with the branch name. | ||
For example: | ||
``` | ||
- name: Remove keys by prefix | ||
run: curl --header "Content-Type: application/json" --request POST --data '{"keyPrefix":"${{ github.head_ref || github.ref_name }}"}' "${ACTIONS_RESULTS_URL}extra/clear_key_prefix" | ||
``` |
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. Instead of adding more functions for the different methods of pruning caches, I would just add filter functionality to the existing |
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. This should probably take the same params as the
I would also prefer a route name like |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { z } from 'zod' | ||
|
||
import { useStorageAdapter } from '~/lib/storage' | ||
|
||
const bodySchema = z.object({ | ||
keyPrefix: z.string().min(1), | ||
}) | ||
|
||
export default defineEventHandler(async (event) => { | ||
const body = (await readBody(event)) as unknown | ||
const parsedBody = bodySchema.safeParse(body) | ||
if (!parsedBody.success) | ||
throw createError({ | ||
statusCode: 400, | ||
statusMessage: `Invalid body: ${parsedBody.error.message}`, | ||
}) | ||
|
||
const { keyPrefix } = parsedBody.data | ||
|
||
const adapter = await useStorageAdapter() | ||
adapter.pruneCacheByKeyPrefix(keyPrefix) | ||
|
||
return { | ||
ok: true, | ||
} | ||
}) |
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 the purpose of the optional custom string?