Cleaning cache for specific object

Options

Hello!

I use RaiseEvent for interacting with specific objects. All events are added to the room cache. But when I finish interacting with this object I don't want to have all raised earlier events to be in cache. All my objects have a unique ID so I want to clear the cache only for objects with this ID. Here is an example of code where I Raise events.

    private void RaiseWebEvent(bool isReliable, ReceiverGroup receiverGroup, params object[] data)
    {
        var hashtableData = new Hashtable {{worldViewID, data}};

        var raiseEventOptions = new RaiseEventOptions
        {
            Receivers = receiverGroup,
            CachingOption = EventCaching.AddToRoomCache
        };

        var sendOptions = new SendOptions
        {
            Reliability = isReliable
        };

        Logging.Log($"Raise event from web view {(string) data[0]}");

        PhotonNetwork.RaiseEvent(NetworkingModel.WebScreenEventCode, hashtableData,
            raiseEventOptions,
            sendOptions);

        receivedEvents.Add(hashtableData);
    }
}

worldviewID is the ID of this specific object.

Then how can I clear all cached events only for this specified object?

Here is my code where I receive events

public async void OnEvent(EventData photonEvent)
{
    if (photonEvent.Code != NetworkingModel.WebScreenEventCode)
        return;

    if (uniqueId == null)
        return;

    if (string.IsNullOrEmpty(uniqueId.Id))
    {
        Logging.LogWarning("unique ID is empty");
        return;
    }

    var hashtableData = (Hashtable) photonEvent.CustomData;

    if (!hashtableData.ContainsKey(uniqueId.Id))
        return;

    var data = (object[]) hashtableData[uniqueId.Id];

    var webEvent = (string) data[0];

    Logging.LogFormat("Received event for web with data {0}", webEvent);

    switch (webEvent)
    {
        case WebEvents.PowerOn:
            await CreateWebView();

            break;

        case WebEvents.TakeControl:
            var userName = (string) data[1];

            SetControlledByUser(userName);
            break;

        case WebEvents.URLChanged:
            var newUrl = (string) data[1];

            await webViewScreen.WaitUntilInitialized();

            webViewScreen.WebView.LoadUrl(newUrl);
            break;

        case WebEvents.Click:
            var normalizedPoint = (Vector2) data[1];

            await webViewScreen.WaitUntilInitialized();

            webViewScreen.WebView.Click(normalizedPoint, true);
            break;

        case WebEvents.Scroll:
            var normalizedScrollDelta = (Vector2) data[1];
            var point = (Vector2) data[2];

            await webViewScreen.WaitUntilInitialized();

            webViewScreen.WebView.Scroll(normalizedScrollDelta, point);
            break;

        case WebEvents.KeyDown:
            var keyDownArguments = JsonConvert.DeserializeObject<KeyboardInputEventArgs>((string) data[1]);

            if (webViewWithKeyDownAndUp != null)
            {
                webViewWithKeyDownAndUp.KeyDown(keyDownArguments.Value, keyDownArguments.Modifiers);
            }
            else
            {
                await webViewScreen.WaitUntilInitialized();

                webViewScreen.WebView.SendKey(keyDownArguments.Value);
            }

            break;

        case WebEvents.KeyUp:
            var keyUpArguments = JsonConvert.DeserializeObject<KeyboardInputEventArgs>((string) data[1]);

            webViewWithKeyDownAndUp?.KeyUp(keyUpArguments.Value, keyUpArguments.Modifiers);

            break;

        case WebEvents.PowerOff:
            PowerOff();

            break;
    }
}


uniqueId.Id == worldviewID


Here in the case PowerOff, I have called this method when I try to remove cached events

public void ClearCachedEvents()
{
    var eventOptions = new RaiseEventOptions
    {
        CachingOption = EventCaching.RemoveFromRoomCache
    };

    Logging.Log($"Events before cleaning {receivedEvents.Count}");

    foreach (var receivedEvent in receivedEvents)
    {
        PhotonNetwork.RaiseEvent(NetworkingModel.WebScreenEventCode, receivedEvent, eventOptions,
            SendOptions.SendReliable);

        var data = (object[]) receivedEvent[worldViewID];

        var webEvent = (string) data[0];

        Logging.Log($"Cleared cached event for {webEvent}");
    }

    Logging.Log("Cleared all cached events from web view");

    receivedEvents.Clear();
}

But it doesn't work. If I log in with a new player I still get a log that I received a cached event

Answers

  • Tobias
    Options

    You can use the sender actorNumber or eventCode as filter.

    Aside from that, you can (only) use a filter of type Hashtable to delete events from the cache. All keys plus all values of said filter must be present and have equal values to remove some item from the event cache.

    Example: Use key (byte)1 to store the ID and make the value some type like byte or int.

    The data for this event can go into another key's value.

    No other type is supported for filtering, sorry.

  • Delpriore
    Options

    So, to clarify your answer, hashtable as a filter only can be used if a key is a byte and a value inside hash is also a byte or int. For another case I can clear my cache only by event code or actorNumber.


    And how to pass sender actorNumber with RaiseEvent to use this later as a filter? Or is this done automatically when I call RaiseEvent function?