Preserve CachedCardCountsForGame across ASF runs

This commit is contained in:
Archi
2022-02-11 00:05:43 +01:00
parent 8e47a5906f
commit fec57e0fff
3 changed files with 135 additions and 6 deletions

View File

@@ -0,0 +1,121 @@
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// |
// Copyright 2015-2022 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
// |
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// |
// http://www.apache.org/licenses/LICENSE-2.0
// |
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using JetBrains.Annotations;
using Newtonsoft.Json;
namespace ArchiSteamFarm.Collections;
[PublicAPI]
public sealed class ObservableConcurrentDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue> where TKey : notnull {
public event EventHandler? OnModified;
public int Count => BackingDictionary.Count;
public bool IsEmpty => BackingDictionary.IsEmpty;
public bool IsReadOnly => false;
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentDictionary<TKey, TValue> BackingDictionary = new();
int ICollection<KeyValuePair<TKey, TValue>>.Count => BackingDictionary.Count;
int IReadOnlyCollection<KeyValuePair<TKey, TValue>>.Count => BackingDictionary.Count;
IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys => BackingDictionary.Keys;
ICollection<TKey> IDictionary<TKey, TValue>.Keys => BackingDictionary.Keys;
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values => BackingDictionary.Values;
ICollection<TValue> IDictionary<TKey, TValue>.Values => BackingDictionary.Values;
public TValue this[TKey key] {
get => BackingDictionary[key];
set {
if (BackingDictionary.TryGetValue(key, out TValue? savedValue) && EqualityComparer<TValue>.Default.Equals(savedValue, value)) {
return;
}
BackingDictionary[key] = value;
OnModified?.Invoke(this, EventArgs.Empty);
}
}
public void Add(KeyValuePair<TKey, TValue> item) {
(TKey key, TValue value) = item;
Add(key, value);
}
public void Add(TKey key, TValue value) => TryAdd(key, value);
public void Clear() {
if (BackingDictionary.IsEmpty) {
return;
}
BackingDictionary.Clear();
OnModified?.Invoke(this, EventArgs.Empty);
}
public bool Contains(KeyValuePair<TKey, TValue> item) => ((ICollection<KeyValuePair<TKey, TValue>>) BackingDictionary).Contains(item);
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) => ((ICollection<KeyValuePair<TKey, TValue>>) BackingDictionary).CopyTo(array, arrayIndex);
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => BackingDictionary.GetEnumerator();
public bool Remove(KeyValuePair<TKey, TValue> item) {
ICollection<KeyValuePair<TKey, TValue>> collection = BackingDictionary;
if (!collection.Remove(item)) {
return false;
}
OnModified?.Invoke(this, EventArgs.Empty);
return true;
}
public bool Remove(TKey key) {
if (!BackingDictionary.TryRemove(key, out _)) {
return false;
}
OnModified?.Invoke(this, EventArgs.Empty);
return true;
}
bool IDictionary<TKey, TValue>.ContainsKey(TKey key) => BackingDictionary.ContainsKey(key);
bool IReadOnlyDictionary<TKey, TValue>.ContainsKey(TKey key) => BackingDictionary.ContainsKey(key);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
bool IReadOnlyDictionary<TKey, TValue>.TryGetValue(TKey key, out TValue value) => BackingDictionary.TryGetValue(key, out value!);
bool IDictionary<TKey, TValue>.TryGetValue(TKey key, out TValue value) => BackingDictionary.TryGetValue(key, out value!);
public bool TryAdd(TKey key, TValue value) {
if (!BackingDictionary.TryAdd(key, value)) {
return false;
}
OnModified?.Invoke(this, EventArgs.Empty);
return true;
}
public bool TryGetValue(TKey key, out TValue? value) => BackingDictionary.TryGetValue(key, out value);
}

View File

@@ -20,7 +20,6 @@
// limitations under the License.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel;
@@ -67,8 +66,6 @@ public sealed class ArchiWebHandler : IDisposable {
[PublicAPI]
public static Uri SteamStoreURL => new("https://store.steampowered.com");
private static readonly ConcurrentDictionary<uint, byte> CachedCardCountsForGame = new();
private static ushort WebLimiterDelay => ASF.GlobalConfig?.WebLimiterDelay ?? GlobalConfig.DefaultWebLimiterDelay;
[PublicAPI]
@@ -1705,7 +1702,7 @@ public sealed class ArchiWebHandler : IDisposable {
throw new ArgumentOutOfRangeException(nameof(appID));
}
if (CachedCardCountsForGame.TryGetValue(appID, out byte result)) {
if (ASF.GlobalDatabase?.CardCountsPerGame.TryGetValue(appID, out byte result) == true) {
return result;
}
@@ -1727,7 +1724,7 @@ public sealed class ArchiWebHandler : IDisposable {
return 0;
}
CachedCardCountsForGame.TryAdd(appID, result);
ASF.GlobalDatabase?.CardCountsPerGame.TryAdd(appID, result);
return result;
}

View File

@@ -28,6 +28,7 @@ using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ArchiSteamFarm.Collections;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Helpers;
using ArchiSteamFarm.Localization;
@@ -48,6 +49,9 @@ public sealed class GlobalDatabase : SerializableFile {
[PublicAPI]
public IReadOnlyDictionary<uint, (uint ChangeNumber, ImmutableHashSet<uint>? AppIDs)> PackagesDataReadOnly => PackagesData;
[JsonProperty(Required = Required.DisallowNull)]
internal readonly ObservableConcurrentDictionary<uint, byte> CardCountsPerGame = new();
[JsonProperty(Required = Required.DisallowNull)]
internal readonly InMemoryServerListProvider ServerListProvider = new();
@@ -107,7 +111,10 @@ public sealed class GlobalDatabase : SerializableFile {
}
[JsonConstructor]
private GlobalDatabase() => ServerListProvider.ServerListUpdated += OnObjectModified;
private GlobalDatabase() {
CardCountsPerGame.OnModified += OnObjectModified;
ServerListProvider.ServerListUpdated += OnObjectModified;
}
[PublicAPI]
public void DeleteFromJsonStorage(string key) {
@@ -159,6 +166,9 @@ public sealed class GlobalDatabase : SerializableFile {
[UsedImplicitly]
public bool ShouldSerializeBackingLastChangeNumber() => LastChangeNumber != 0;
[UsedImplicitly]
public bool ShouldSerializeCardCountsPerGame() => !CardCountsPerGame.IsEmpty;
[UsedImplicitly]
public bool ShouldSerializeKeyValueJsonStorage() => !KeyValueJsonStorage.IsEmpty;
@@ -174,6 +184,7 @@ public sealed class GlobalDatabase : SerializableFile {
protected override void Dispose(bool disposing) {
if (disposing) {
// Events we registered
CardCountsPerGame.OnModified -= OnObjectModified;
ServerListProvider.ServerListUpdated -= OnObjectModified;
// Those are objects that are always being created if constructor doesn't throw exception