-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Make expression handler stateless
Signed-off-by: Sagilio <Sagilio@outlook.com>
- Loading branch information
Showing
25 changed files
with
674 additions
and
381 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Casbin.Caching; | ||
|
||
public interface IExpressionCache<T> : IExpressionCache | ||
{ | ||
public bool TryGet(string expressionString, out T t); | ||
|
||
public void Set(string expressionString, T t); | ||
} | ||
|
||
public interface IExpressionCache | ||
{ | ||
public void Clear(); | ||
} |
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,10 @@ | ||
namespace Casbin.Caching; | ||
|
||
public interface IGFunctionCache | ||
{ | ||
public void Set(string name1, string name2, bool result, string domain = null); | ||
|
||
public bool TryGet(string name1, string name2, out bool result, string domain = null); | ||
|
||
public void Clear(); | ||
} |
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,8 @@ | ||
namespace Casbin.Caching; | ||
|
||
public interface IGFunctionCachePool | ||
{ | ||
public IGFunctionCache GetCache(string roleType); | ||
|
||
public void Clear(string roleType); | ||
} |
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 |
---|---|---|
@@ -1,27 +1,10 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using DynamicExpresso; | ||
|
||
namespace Casbin.Evaluation | ||
{ | ||
public interface IExpressionHandler | ||
{ | ||
public EnforceContext EnforceContext { get; } | ||
|
||
public IDictionary<string, Parameter> Parameters { get; } | ||
|
||
public void SetEnforceContext(in EnforceContext context); | ||
|
||
public void SetFunction(string name, Delegate function); | ||
namespace Casbin.Evaluation; | ||
|
||
public void SetGFunctions(); | ||
|
||
public void EnsureCreated(string expressionString, IReadOnlyList<object> requestValues); | ||
|
||
public bool Invoke(string expressionString, IReadOnlyList<object> requestValues); | ||
|
||
public void SetRequestParameters(IReadOnlyList<object> requestValues); | ||
public interface IExpressionHandler | ||
{ | ||
public void SetFunction(string name, Delegate function); | ||
|
||
public void SetPolicyParameters(IReadOnlyList<string> policyValues); | ||
} | ||
public bool Invoke(in EnforceContext context, ref EnforceSession session); | ||
} |
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 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using DynamicExpresso; | ||
|
||
namespace Casbin.Caching; | ||
|
||
internal class ExpressionCache : IExpressionCache<Lambda> | ||
{ | ||
private readonly Lazy<Dictionary<string, Lambda>> _cache = new(); | ||
|
||
public bool TryGet(string expressionString, out Lambda lambda) | ||
{ | ||
return _cache.Value.TryGetValue(expressionString, out lambda); | ||
} | ||
|
||
public void Set(string expressionString, Lambda lambda) | ||
{ | ||
_cache.Value[expressionString] = lambda; | ||
} | ||
|
||
public void Clear() => _cache.Value.Clear(); | ||
} | ||
|
||
internal class ExpressionCache<TFunc> : IExpressionCache<TFunc> where TFunc : Delegate | ||
{ | ||
private readonly Lazy<Dictionary<string, TFunc>> _cache = new(); | ||
|
||
public bool TryGet(string expressionString, out TFunc func) | ||
{ | ||
return _cache.Value.TryGetValue(expressionString, out func); | ||
} | ||
|
||
public void Set(string expressionString, TFunc func) | ||
{ | ||
_cache.Value[expressionString] = func; | ||
} | ||
|
||
public void Clear() => _cache.Value.Clear(); | ||
} |
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,31 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Casbin.Caching; | ||
|
||
public class GFunctionCache : IGFunctionCache | ||
{ | ||
private readonly Dictionary<string, bool> _cache = new(); | ||
|
||
public void Set(string name1, string name2, bool result, string domain = null) | ||
{ | ||
_cache[Key(name1, name2, domain)] = result; | ||
} | ||
|
||
public bool TryGet(string name1, string name2, out bool result, string domain = null) | ||
{ | ||
return _cache.TryGetValue(Key(name1, name2, domain), out result); | ||
} | ||
|
||
private static string Key(string name1, string name2, string domain = null) | ||
{ | ||
bool hasDomain = domain is not null; | ||
return hasDomain | ||
? string.Join(":", name1, name2, domain) | ||
: string.Join(":", name1, name2); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
_cache.Clear(); | ||
} | ||
} |
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,28 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Casbin.Caching; | ||
|
||
public class GFunctionCachePool : IGFunctionCachePool | ||
{ | ||
private readonly Dictionary<string, IGFunctionCache> _cachePool = new(); | ||
|
||
public void Clear(string roleType) | ||
{ | ||
if (_cachePool.TryGetValue(roleType, out IGFunctionCache cache)) | ||
{ | ||
cache.Clear(); | ||
} | ||
} | ||
|
||
public IGFunctionCache GetCache(string roleType) | ||
{ | ||
if (_cachePool.TryGetValue(roleType, out IGFunctionCache cache)) | ||
{ | ||
return cache; | ||
} | ||
|
||
cache = new GFunctionCache(); | ||
_cachePool[roleType] = cache; | ||
return cache; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
<!--EXTERNAL_PROPERTIES: TargetFramework--> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
|
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
Oops, something went wrong.