locked
How to implement a cache for SignalR? RRS feed

  • Question

  • User383393697 posted

    HI, I'm learning C# and to do that I am building a text based RPG using Signalr. I can create a player and save the data as a JSON file. But what I want to do is store that data in a dictionary using the Connection ID as the key. This way any action on the player data I can get it from the Dictionary and not re-read the JSON file every time.

    I created a dictionary to do this and also used the singleton pattern to do it. When I run the code the player info gets stored in the dictionary. I then call another method to retrieve the data but the dictionary is now null.

    I'm really confused as to how? It's like every call to the hub creates a new instance. How can I store this data in memory? 

    Thanks.

    Wednesday, May 4, 2016 7:38 PM

Answers

  • User61956409 posted

    Hi riak,

    what I want to do is store that data in a dictionary using the Connection ID as the key.

    I’d like to know when and on which event you store the data with connection id in dictionary. Could you share us a sample code that could reproduce the issue?

    Besides, you could check the code that explained how to retain connection and user information in a dictionary in this link.

    http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections#inmemory

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 5, 2016 2:36 AM