hyperoslo / Cache

:package: Nothing but Cache.
Other
2.96k stars 335 forks source link

Unable to cache object to Disk #310

Closed christopherkarani closed 5 months ago

christopherkarani commented 1 year ago

When caching objects

func setupStorage() {
            do {
                let memoryConfig = MemoryConfig(expiry: .never)
                let memoryStorage = MemoryStorage<String, [Lesson]>(config: memoryConfig)
                let diskConfig = DiskConfig(name: CacheKey.diskConfig, directory: fileUrl())
                let diskStorage = try DiskStorage<String, [Lesson]>(
                    config: diskConfig,
                    transformer: TransformerFactory.forCodable(ofType: [Lesson].self)
                )
                let hybridStorage = HybridStorage(memoryStorage: memoryStorage, diskStorage: diskStorage)
                self.storage = Storage(hybridStorage: hybridStorage)
            } catch {
                self.error = AppError.cacheError(description: error.localizedDescription)
            }
        }
private func fetchLessons() async {
            do {
                let lessons =  try await networkService.fetchLessons()
                self.lessons = lessons
                cache.insert(lessons, forKey: CacheKey.key)
                try storage?.setObject(lessons, forKey: CacheKey.key, expiry: .never)
                let entry = try storage?.entry(forKey: CacheKey.key)
                print(entry?.filePath)
            } catch {
                self.error = AppError.errorFetchingLessons(description: error.localizedDescription)
            }
        }

entry?.filePath is always nil

let doesExist = storage!.objectExists(forKey: CacheKey.key) always return false when app is closed and open again

even when I don't manually add the directory to the DiskConfig this still happens

3lvis commented 5 months ago

Sorry for not being helpful before, I hope you managed to fix your issue.

3lvis commented 5 months ago

Closing, reason outdated issue.