Skip to content

Commit

Permalink
improve: change cachedapi to use generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheng JIANG committed Aug 30, 2020
1 parent 9c67ba4 commit c88f13c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "casbin"
version = "2.0.0"
version = "2.0.1"
authors = ["Joey <[email protected]>", "Cheng JIANG <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Add this package to `Cargo.toml` of your project. (Check https://crates.io/crate

```toml
[dependencies]
casbin = { version = "2.0.0", default-features = false, features = ["runtime-async-std", "logging"] }
casbin = { version = "2.0.1", default-features = false, features = ["runtime-async-std", "logging"] }
async-std = { version = "1.5.0", features = ["attributes"] }
```

Expand Down
12 changes: 9 additions & 3 deletions src/cached_api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use crate::{cache::Cache, core_api::CoreApi};

pub trait CachedApi: CoreApi + Send + Sync {
fn get_mut_cache(&mut self) -> &mut dyn Cache<u64, bool>;
fn set_cache(&mut self, cache: Box<dyn Cache<u64, bool>>);
use std::hash::Hash;

pub trait CachedApi<K, V>: CoreApi + Send + Sync
where
K: Eq + Hash,
V: Clone,
{
fn get_mut_cache(&mut self) -> &mut dyn Cache<K, V>;
fn set_cache(&mut self, cache: Box<dyn Cache<K, V>>);
fn set_capacity(&mut self, cap: usize);
}
2 changes: 1 addition & 1 deletion src/cached_enforcer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ impl CoreApi for CachedEnforcer {
}
}

impl CachedApi for CachedEnforcer {
impl CachedApi<u64, bool> for CachedEnforcer {
fn get_mut_cache(&mut self) -> &mut dyn Cache<u64, bool> {
&mut *self.cache
}
Expand Down
5 changes: 4 additions & 1 deletion src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ pub(crate) fn notify_logger_and_watcher<T: CoreApi>(e: &mut T, d: EventData) {

#[cfg(feature = "cached")]
#[allow(unused_variables)]
pub(crate) fn clear_cache<T: CoreApi + CachedApi>(ce: &mut T, d: EventData) {
pub(crate) fn clear_cache<T: CoreApi + CachedApi<u64, bool>>(
ce: &mut T,
d: EventData,
) {
#[cfg(feature = "logging")]
{
ce.get_logger().print_mgmt_log(&d);
Expand Down

1 comment on commit c88f13c

@GopherJ
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust Benchmark

Benchmark suite Current: c88f13c Previous: 9c67ba4 Ratio
b_benchmark_abac_model 6252 ns/iter (± 870) 5447 ns/iter (± 867) 1.15
b_benchmark_basic_model 7092 ns/iter (± 624) 6216 ns/iter (± 1031) 1.14
b_benchmark_cached_abac_model 1275 ns/iter (± 132) 1149 ns/iter (± 268) 1.11
b_benchmark_cached_key_match 584 ns/iter (± 58) 567 ns/iter (± 139) 1.03
b_benchmark_cached_priority_model 615 ns/iter (± 68) 549 ns/iter (± 206) 1.12
b_benchmark_cached_rbac_model 620 ns/iter (± 79) 537 ns/iter (± 121) 1.15
b_benchmark_cached_rbac_model_large 555 ns/iter (± 65) 495 ns/iter (± 69) 1.12
b_benchmark_cached_rbac_model_medium 571 ns/iter (± 87) 551 ns/iter (± 102) 1.04
b_benchmark_cached_rbac_model_small 609 ns/iter (± 70) 576 ns/iter (± 111) 1.06
b_benchmark_cached_rbac_model_with_domains 861 ns/iter (± 76) 949 ns/iter (± 133) 0.91
b_benchmark_cached_rbac_with_deny 625 ns/iter (± 59) 587 ns/iter (± 92) 1.06
b_benchmark_cached_rbac_with_resource_roles 635 ns/iter (± 92) 595 ns/iter (± 83) 1.07
b_benchmark_key_match 26815 ns/iter (± 3878) 26783 ns/iter (± 3312) 1.00
b_benchmark_priority_model 8028 ns/iter (± 935) 7252 ns/iter (± 1353) 1.11
b_benchmark_raw 9 ns/iter (± 1) 7 ns/iter (± 1) 1.29
b_benchmark_rbac_model 10333 ns/iter (± 1139) 9933 ns/iter (± 1916) 1.04
b_benchmark_rbac_model_large 18775828 ns/iter (± 2707344) 18030250 ns/iter (± 2233963) 1.04
b_benchmark_rbac_model_medium 1927724 ns/iter (± 208258) 1822806 ns/iter (± 254048) 1.06
b_benchmark_rbac_model_small 181896 ns/iter (± 18082) 182270 ns/iter (± 38924) 1.00
b_benchmark_rbac_model_with_domains 10873 ns/iter (± 1445) 9898 ns/iter (± 2079) 1.10
b_benchmark_rbac_with_deny 14506 ns/iter (± 1869) 13323 ns/iter (± 2028) 1.09
b_benchmark_rbac_with_resource_roles 8366 ns/iter (± 960) 8019 ns/iter (± 2075) 1.04
b_benchmark_role_manager_large 8745189 ns/iter (± 999704) 8640797 ns/iter (± 1005575) 1.01
b_benchmark_role_manager_medium 809292 ns/iter (± 121665) 700620 ns/iter (± 107941) 1.16
b_benchmark_role_manager_small 87403 ns/iter (± 12381) 76701 ns/iter (± 15764) 1.14
b_benmark_cached_basic_model 633 ns/iter (± 92) 596 ns/iter (± 115) 1.06

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.