File indexing completed on 2024-05-12 04:42:32

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KPUBLICTRANSPORT_CACHE_H
0008 #define KPUBLICTRANSPORT_CACHE_H
0009 
0010 #include "kpublictransport_export.h"
0011 
0012 #include <KPublicTransport/Attribution>
0013 
0014 #include <chrono>
0015 #include <vector>
0016 
0017 class QString;
0018 
0019 namespace KPublicTransport {
0020 
0021 class Journey;
0022 class Location;
0023 class Stopover;
0024 
0025 enum class CacheHitType {
0026     Miss,
0027     Positive,
0028     Negative
0029 };
0030 
0031 template <typename T> struct CacheEntry
0032 {
0033     CacheEntry() = default;
0034     CacheEntry(CacheEntry&&) = default;
0035     CacheEntry(const CacheEntry&) = delete;
0036     ~CacheEntry() = default;
0037     CacheEntry& operator=(CacheEntry&&) = default;
0038     CacheEntry& operator=(const CacheEntry&) = delete;
0039 
0040     std::vector<T> data;
0041     std::vector<Attribution> attributions;
0042     CacheHitType type = CacheHitType::Miss;
0043 };
0044 
0045 /** C++20 forward compatibility */
0046 namespace chrono {
0047     typedef std::chrono::duration<std::chrono::seconds::rep, std::ratio<86400>> days;
0048 }
0049 
0050 /** Query result caching functions.
0051  *  @internal exported for unit tests only
0052  */
0053 namespace Cache
0054 {
0055     /** Add data and corresponding attribution information to the cache. */
0056     KPUBLICTRANSPORT_EXPORT void addLocationCacheEntry(const QString &backendId, const QString &cacheKey, const std::vector<Location> &data, const std::vector<Attribution> &attributions, std::chrono::seconds ttl = chrono::days(30));
0057     /** Add negative cache entry for location queries, ie. remember a result could not be found. */
0058     KPUBLICTRANSPORT_EXPORT void addNegativeLocationCacheEntry(const QString &backendId, const QString &cacheKey, std::chrono::seconds ttl = chrono::days(30));
0059     /** Perform cache lookup for location results. */
0060     KPUBLICTRANSPORT_EXPORT CacheEntry<Location> lookupLocation(const QString &backendId, const QString &cacheKey);
0061 
0062     /** Add negative cache entry for departure queries, ie. remember a result could not be found. */
0063     void addNegativeDepartureCacheEntry(const QString &backendId, const QString &cacheKey, std::chrono::seconds ttl = chrono::days(30));
0064     /** Perform cache lookup for departure results. */
0065     CacheEntry<Stopover> lookupDeparture(const QString &backendId, const QString &cacheKey);
0066 
0067     /** Add negative cache entry for departure queries, ie. remember a result could not be found. */
0068     void addNegativeJourneyCacheEntry(const QString &backendId, const QString &cacheKey, std::chrono::seconds ttl = chrono::days(30));
0069     /** Perform cache lookup for departure results. */
0070     CacheEntry<Journey> lookupJourney(const QString &backendId, const QString &cacheKey);
0071 
0072     /** Add a positive cache entry for a vehicle layout query. */
0073     void addVehicleLayoutCacheEntry(const QString &backendId, const QString &cacheKey, const Stopover &data, const std::vector<Attribution> &attributions, std::chrono::seconds ttl = std::chrono::minutes(1));
0074     /** Add negative cache entry for vehicle layout queries. */
0075     void addNegativeVehicleLayoutCacheEntry(const QString &backendId, const QString &cacheKey, std::chrono::seconds ttl = chrono::days(30));
0076     /** Perform cache lookup for vehicle layout results. */
0077     CacheEntry<Stopover> lookupVehicleLayout(const QString &backendId, const QString &cacheKey);
0078 
0079     /** Returns all cached attribution information.
0080      *  The result is inserted in the assumed to be sorted @p attrs and deduplicated.
0081      */
0082     KPUBLICTRANSPORT_EXPORT void allCachedAttributions(std::vector<Attribution> &attrs);
0083 
0084     /** Expire old cache entries. */
0085     void KPUBLICTRANSPORT_EXPORT expire();
0086 }
0087 
0088 }
0089 
0090 #endif // KPUBLICTRANSPORT_CACHE_H