Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ jobs:
uses: dtolnay/rust-toolchain@stable

- name: Install Aeron Cache Backend
env:
HOMEBREW_NO_AUTO_UPDATE: "1"
HOMEBREW_NO_INSTALL_CLEANUP: "1"
HOMEBREW_NO_REQUIRE_TAP_TRUST: "1"
run: |
brew tap bhf/aeroncache
brew install aeron-cache
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,36 @@ By default, the CLI assumes that your backend cache service is reachable at `htt
## Available commands

It exposes the following commands:

### Cache commands
- `create <name>`: Create a new cache
- `delete <name>`: Delete a cache
- `get-cache <name>`: Get all items from a cache
- `clear-cache <name>`: Clear all items from a cache
- `insert <name> <key> <value>`: Insert an item into a cache
- `insert-timed <name> <key> <value> <ttl>`: Insert an item with a time-to-live (milliseconds) into a cache
- `get <name> <key>`: Get an item from a cache
- `remove <name> <key>`: Remove an item from a cache
- `cancel-removal <name> <key>`: Cancel a scheduled removal of an item
- `list-caches`: List all available caches and their item count
- `stats`: Get global cache statistics

### Counter commands
- `create-counter-cache <name>`: Create a new counter cache
- `delete-counter-cache <name>`: Delete a counter cache
- `get-counter-cache <name>`: Get all counters from a counter cache
- `clear-counter-cache <name>`: Clear all counters from a counter cache
- `put-counter <name> <key> <value>`: Put a counter into a counter cache
- `put-timed-counter <name> <key> <value> <ttl>`: Put a counter with a time-to-live (milliseconds) into a counter cache
- `get-counter <name> <key>`: Get a single counter from a counter cache
- `delete-counter <name> <key>`: Delete a single counter from a counter cache
- `increment-counter <name> <key> [amount]`: Increment a counter (defaults to 1)
- `decrement-counter <name> <key> [amount]`: Decrement a counter (defaults to 1)
- `set-counter <name> <key> <value>`: Set a counter to a given value
- `cancel-counter-removal <name> <key>`: Cancel a scheduled removal of a counter
- `list-counter-caches`: List all available counter caches and their item count
- `counter-stats`: Get global counter cache statistics

Use the `--help` flag anytime for more information.

Example: `cargo run -- create mycache`
Expand Down
8 changes: 8 additions & 0 deletions src/cacherequests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ pub(crate) struct CreateRequest<'a> {
pub(crate) struct PutItemRequest<'a> {
pub(crate) key: &'a str,
pub(crate) value: &'a str,
}

#[allow(non_snake_case)]
#[derive(Serialize)]
pub(crate) struct PutTimedItemRequest<'a> {
pub(crate) key: &'a str,
pub(crate) value: &'a str,
pub(crate) ttl: i64,
}
30 changes: 30 additions & 0 deletions src/cacheresponses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,33 @@ pub(crate) struct CacheStatsResponse {
pub(crate) totalItemsCount: i32,
pub(crate) errorCount: i32,
}

#[allow(non_snake_case)]
#[derive(serde::Deserialize, Debug)]
pub(crate) struct PutTimedItemResponse {
pub(crate) cacheId: String,
pub(crate) key: String,
pub(crate) operationStatus: String,
}

#[derive(serde::Deserialize, Debug)]
#[serde(untagged)]
pub(crate) enum PutTimedItemResult {
Ok(PutTimedItemResponse),
Err(ErrorResponse),
}

#[allow(non_snake_case)]
#[derive(serde::Deserialize, Debug)]
pub(crate) struct CancelItemRemovalResponse {
pub(crate) cacheId: String,
pub(crate) key: String,
pub(crate) operationStatus: String,
}

#[derive(serde::Deserialize, Debug)]
#[serde(untagged)]
pub(crate) enum CancelItemRemovalResult {
Ok(CancelItemRemovalResponse),
Err(ErrorResponse),
}
77 changes: 75 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::cacherequests::{CreateRequest, PutItemRequest};
use crate::cacherequests::{CreateRequest, PutItemRequest, PutTimedItemRequest};
use crate::cacheresponses::{
CreateCacheResult, DeleteCacheResult, DeleteItemResult, GetItemResult, PutItemResult,
CancelItemRemovalResult, CreateCacheResult, DeleteCacheResult, DeleteItemResult, GetItemResult,
PutItemResult, PutTimedItemResult,
};
use reqwest::blocking::Client;
use reqwest::header::CONTENT_TYPE;
Expand Down Expand Up @@ -129,6 +130,78 @@ pub(crate) fn process_insert_item(
Ok(())
}

pub(crate) fn process_put_timed_item(
rest_client: &Client,
aeron_cache_api_url: &str,
cache_name: &String,
key: &String,
value: &String,
ttl: i64,
) -> Result<(), Box<dyn Error>> {
let put_timed_item_request = PutTimedItemRequest {
key,
value,
ttl,
};

let url = &format!("{}/cache/timed/{}", aeron_cache_api_url, cache_name);

let put_timed_item_response = rest_client
.post(url)
.header(CONTENT_TYPE, "application/json")
.body(serde_json::to_string(&put_timed_item_request)?)
.send()?;

let body = put_timed_item_response.text()?;
let result: PutTimedItemResult = serde_json::from_str(&body)?;

match result {
PutTimedItemResult::Ok(resp) => println!(
"Put timed item into cache {} on key {} with ttl {}ms",
resp.cacheId, resp.key, ttl
),
PutTimedItemResult::Err(err) => {
println!("Error: {}", err.errorMsg);
println!("Help: {}", err.helpMsg);
}
}

Ok(())
}

pub(crate) fn process_cancel_item_removal(
rest_client: &Client,
aeron_cache_api_url: &str,
cache_name: &String,
key: &String,
) -> Result<(), Box<dyn Error>> {
let url = &format!(
"{}/cache/{}/{}/cancel-removal",
aeron_cache_api_url, cache_name, key
);

let response = rest_client
.post(url)
.header(CONTENT_TYPE, "application/json")
.send()?;

let body = response.text()?;
let result: CancelItemRemovalResult = serde_json::from_str(&body)?;

match result {
CancelItemRemovalResult::Ok(resp) => println!(
"Cancelled scheduled removal of item in cache {} on key {}",
resp.cacheId, resp.key
),
CancelItemRemovalResult::Err(err) => {
println!("Error: {}", err.errorMsg);
println!("Help: {}", err.helpMsg);
}
}

Ok(())
}

pub(crate) fn process_create_cache(
rest_client: &Client,
aeron_cache_api_url: &str,
Expand Down
Loading
Loading