Skip to content

Commit

Permalink
Restructured and cleaned up Server implemenations of processQuery and…
Browse files Browse the repository at this point in the history
… processCommand; created MemoryCache extensions for use around Server and Actors.
  • Loading branch information
ScottArbeit committed Jul 25, 2024
1 parent 578ac82 commit f094ef6
Show file tree
Hide file tree
Showing 20 changed files with 529 additions and 370 deletions.
136 changes: 136 additions & 0 deletions src/Grace.Actors/Extensions/MemoryCache.Extensions.Actor.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
namespace Grace.Actors.Extensions

open Grace.Shared.Constants
open Grace.Shared.Types
open Microsoft.Extensions.Caching.Memory
open System

module MemoryCache =
[<Literal>]
let ownerIdPrefix = "OwI"
[<Literal>]
let organizationIdPrefix = "OrI"
[<Literal>]
let repositoryIdPrefix = "ReI"
[<Literal>]
let branchIdPrefix = "BrI"
[<Literal>]
let ownerNamePrefix = "OwN"
[<Literal>]
let organizationNamePrefix = "OrN"
[<Literal>]
let repositoryNamePrefix = "ReN"
[<Literal>]
let branchNamePrefix = "BrN"

type Microsoft.Extensions.Caching.Memory.IMemoryCache with

/// Create a new entry in MemoryCache with a default expiration time.
member this.CreateWithDefaultExpirationTime (key: string) value =
use newCacheEntry = this.CreateEntry(key, Value = value, AbsoluteExpirationRelativeToNow = MemoryCache.DefaultExpirationTime)
()

/// Create a new entry in MemoryCache to confirm that an OwnerId exists.
member this.CreateOwnerIdEntry (ownerId: OwnerId) (value: string) =
this.CreateWithDefaultExpirationTime $"{ownerIdPrefix}:{ownerId}" value

/// Create a new entry in MemoryCache to confirm that an OrganizationId exists.
member this.CreateOrganizationIdEntry (organizationId: OrganizationId) (value: string) =
this.CreateWithDefaultExpirationTime $"{organizationIdPrefix}:{organizationId}" value

/// Create a new entry in MemoryCache to confirm that a RepositoryId exists.
member this.CreateRepositoryIdEntry (repositoryId: RepositoryId) (value: string) =
this.CreateWithDefaultExpirationTime $"{repositoryIdPrefix}:{repositoryId}" value

/// Create a new entry in MemoryCache to confirm that a BranchId exists.
member this.CreateBranchIdEntry (branchId: BranchId) (value: string) =
this.CreateWithDefaultExpirationTime $"{branchIdPrefix}:{branchId}" value

/// Create a new entry in MemoryCache to link an OwnerName with an OwnerId.
member this.CreateOwnerNameEntry (ownerName: OwnerName) (ownerId: OwnerId) =
this.CreateWithDefaultExpirationTime $"{ownerNamePrefix}:{ownerName}" ownerId

/// Create a new entry in MemoryCache to link an OrganizationName with an OrganizationId.
member this.CreateOrganizationNameEntry (organizationName: OrganizationName) (organizationId: OrganizationId) =
this.CreateWithDefaultExpirationTime $"{organizationNamePrefix}:{organizationName}" organizationId

/// Create a new entry in MemoryCache to link a RepositoryName with a RepositoryId.
member this.CreateRepositoryNameEntry(repositoryName: RepositoryName) (repositoryId: RepositoryId) =
this.CreateWithDefaultExpirationTime $"{repositoryNamePrefix}:{repositoryName}" repositoryId

/// Create a new entry in MemoryCache to link a BranchName with a BranchId.
member this.CreateBranchNameEntry(branchName: string) (branchId: BranchId) =
this.CreateWithDefaultExpirationTime $"{branchNamePrefix}:{branchName}" branchId

/// Get a value from MemoryCache, if it exists.
member this.GetFromCache<'T> (key: string) =
let mutable value = Unchecked.defaultof<'T>
if this.TryGetValue(key, &value) then
Some value
else
None

/// Check if we have an entry in MemoryCache for an OwnerId, and return the OwnerId if we have it.
member this.GetOwnerIdEntry(ownerId: OwnerId) =
this.GetFromCache<string> $"{ownerIdPrefix}:{ownerId}"

/// Check if we have an entry in MemoryCache for an OrganizationId, and return the OrganizationId if we have it.
member this.GetOrganizationIdEntry(organizationId: OrganizationId) =
this.GetFromCache<string> $"{organizationIdPrefix}:{organizationId}"

/// Check if we have an entry in MemoryCache for a RepositoryId, and return the RepositoryId if we have it.
member this.GetRepositoryIdEntry(repositoryId: RepositoryId) =
this.GetFromCache<string> $"{repositoryIdPrefix}:{repositoryId}"

/// Check if we have an entry in MemoryCache for a BranchId, and return the BranchId if we have it.
member this.GetBranchIdEntry(branchId: BranchId) =
this.GetFromCache<string> $"{branchIdPrefix}:{branchId}"

/// Check if we have an entry in MemoryCache for an OwnerName, and return the OwnerId if we have it.
member this.GetOwnerNameEntry(ownerName: string) =
this.GetFromCache<Guid> $"{ownerNamePrefix}:{ownerName}"

/// Check if we have an entry in MemoryCache for an OrganizationName, and return the OrganizationId if we have it.
member this.GetOrganizationNameEntry(organizationName: string) =
this.GetFromCache<Guid> $"{organizationNamePrefix}:{organizationName}"

/// Check if we have an entry in MemoryCache for a RepositoryName, and return the RepositoryId if we have it.
member this.GetRepositoryNameEntry(repositoryName: string) =
this.GetFromCache<Guid> $"{repositoryNamePrefix}:{repositoryName}"

/// Check if we have an entry in MemoryCache for a BranchName, and return the BranchId if we have it.
member this.GetBranchNameEntry(branchName: string) =
this.GetFromCache<Guid> $"{branchNamePrefix}:{branchName}"


/// Remove an entry in MemoryCache for an OwnerId.
member this.RemoveOwnerIdEntry(ownerId: OwnerId) =
this.Remove($"{ownerIdPrefix}:{ownerId}")

/// Remove an entry in MemoryCache for an OrganizationId.
member this.RemoveOrganizationIdEntry(organizationId: OrganizationId) =
this.Remove($"{organizationIdPrefix}:{organizationId}")

/// Remove an entry in MemoryCache for a RepositoryId.
member this.RemoveRepositoryIdEntry(repositoryId: RepositoryId) =
this.Remove($"{repositoryIdPrefix}:{repositoryId}")

/// Remove an entry in MemoryCache for a BranchId.
member this.RemoveBranchIdEntry(branchId: BranchId) =
this.Remove($"{branchIdPrefix}:{branchId}")

/// Remove an entry in MemoryCache for an OwnerName.
member this.RemoveOwnerNameEntry(ownerName: string) =
this.Remove($"{ownerNamePrefix}:{ownerName}")

/// Remove an entry in MemoryCache for an OrganizationName.
member this.RemoveOrganizationNameEntry(organizationName: string) =
this.Remove($"{organizationNamePrefix}:{organizationName}")

/// Remove an entry in MemoryCache for a RepositoryName.
member this.RemoveRepositoryNameEntry(repositoryName: string) =
this.Remove($"{repositoryNamePrefix}:{repositoryName}")

/// Remove an entry in MemoryCache for a BranchName.
member this.RemoveBranchNameEntry(branchName: string) =
this.Remove($"{branchNamePrefix}:{branchName}")
1 change: 1 addition & 0 deletions src/Grace.Actors/Grace.Actors.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="Extensions\MemoryCache.Extensions.Actor.fs" />
<Compile Include="Constants.Actor.fs" />
<Compile Include="Storage.Actor.fs" />
<Compile Include="Commands.Actor.fs" />
Expand Down
8 changes: 4 additions & 4 deletions src/Grace.Actors/Owner.Actor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ open Dapr.Actors
open Dapr.Actors.Runtime
open FSharp.Control
open Grace.Actors.Commands.Owner
open Grace.Actors.Services
open Grace.Actors.Events.Owner
open Grace.Actors.Extensions.MemoryCache
open Grace.Actors.Services
open Grace.Actors.Interfaces
open Grace.Shared
open Grace.Shared.Constants
Expand Down Expand Up @@ -342,13 +343,12 @@ module Owner =
// Clear the OwnerNameActor for the old name.
let ownerNameActor = actorProxyFactory.CreateActorProxy<IOwnerNameActor>(ActorId(ownerDto.OwnerName), ActorName.OwnerName)
do! ownerNameActor.ClearOwnerId metadata.CorrelationId
memoryCache.Remove($"OwN:{ownerDto.OwnerName}")
memoryCache.RemoveOwnerNameEntry ownerDto.OwnerName

// Set the OwnerNameActor for the new name.
let ownerNameActor = actorProxyFactory.CreateActorProxy<IOwnerNameActor>(ActorId(newName), ActorName.OwnerName)
do! ownerNameActor.SetOwnerId ownerDto.OwnerId metadata.CorrelationId
use newCacheEntry =
memoryCache.CreateEntry($"OwN:{newName}", Value = ownerDto.OwnerId, AbsoluteExpirationRelativeToNow = MemoryCache.DefaultExpirationTime)
memoryCache.CreateOwnerNameEntry newName ownerDto.OwnerId

return Ok(OwnerEventType.NameSet newName)
| OwnerCommand.SetType ownerType -> return Ok(OwnerEventType.TypeSet ownerType)
Expand Down
Loading

0 comments on commit f094ef6

Please sign in to comment.