question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In memory example configuration

See original GitHub issue

Hey, a while ago I was playing around with CosmoStore and I used the in-memory approach.

#load ".paket/load/main.group.fsx" 

open System
open System.Collections.Concurrent
open CosmoStore
open CosmoStore.InMemory
open Thoth.Json.Net
open Microsoft.FSharp.Reflection

let config : Configuration =
    { InMemoryStreams = ConcurrentDictionary<string,Stream>()
      InMemoryEvents = ConcurrentDictionary<Guid, EventRead>() }
    
let store = EventStore.getEventStore config

type Event =
    | Increase of int
    | Decrease of int

let encodeEvent = Encode.Auto.generateEncoder<Event>()
let decodeEvent = Decode.Auto.generateDecoder<Event>()

let getUnionCaseName (x:'a) = 
    match FSharpValue.GetUnionFields(x, typeof<'a>) with
    | case, _ -> case.Name  

let createEvent event =
    { Id = (Guid.NewGuid())
      CorrelationId = None
      CausationId = None
      Name = getUnionCaseName event
      Data = encodeEvent event
      Metadata = None }
    
let appendEvent event =
    store.AppendEvent "CounterStream" ExpectedPosition.Any event
    |> Async.AwaitTask
    |> Async.RunSynchronously

let increase amount =
    Increase(amount)
    |> createEvent
    |> appendEvent
    
let decrease amount =
    Decrease(amount)
    |> createEvent
    |> appendEvent

increase 5
decrease 3
increase 198
decrease 203
increase 4

store.GetEvents "CounterStream" EventsReadRange.AllEvents
|> Async.AwaitTask
|> Async.RunSynchronously
|> List.fold (fun acc e ->
    match Decode.fromValue "$" decodeEvent e.Data with
    | Ok(Increase i) -> acc + i
    | Ok(Decrease d) -> acc - d
    | Error e ->
        failwithf "invalid event from store, %s" e
) 0
|> printfn "Projected state is %d"

When upgrading to v3 I noticed that my configuration isn’t correct anymore. Could a sample perhaps be added to the README for InMemory configuration?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Dzoukrcommented, Jan 11, 2020

My fault, I didn’t publish new in memory version yet - will do now.

0reactions
kunjee17commented, Jan 11, 2020

@Dzoukr this can be closed as well. I guess we are having updated version now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keeping Configuration Settings in Memory
This lets me organize my configuration settings into sections. My example defined a section called toDoService with two settings: url and ...
Read more >
How to create a hierarchical in-memory configuration ...
I would like to create a hierarchical in-memory configuration along the lines of using a nested dictionary to populate the IConfiguration . My ......
Read more >
In-Memory Authentication
In the following sample, we use User.withDefaultPasswordEncoder to ensure that the password stored in memory is protected.
Read more >
Configuration in ASP.NET Core
Learn how to use the Configuration API to configure AppSettings in an ASP.NET Core app.
Read more >
MemoryConfigurationSource Class
Builds the MemoryConfigurationProvider for this source. Equals(Object). Determines whether the specified object is equal to the current object.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found