1 Memory
config.prefer(MemoryKeyedCache.self, for: KeyedCache.self)
对于使用内存cache存储很简直接设置使用即可
2 DB
使用数据作为cache就比较麻烦--设置对应数据库 注册对应的表enty
2.1 选择数据作为存储
config.prefer(IACache.self, for: KeyedCache.self) //< 数据库作为cache
为了后续修改方便我们进行了alias ```typealias IACache = SQLiteCache```
其实内部也是进行了一层的alias```public typealias SQLiteCache = DatabaseKeyedCache<ConfiguredDatabase<SQLiteDatabase>>```
顺着内部走```public final class DatabaseKeyedCache<Database>: KeyedCache, Service
where Database: KeyedCacheSupporting```
可以看出作为DatabaseKeyedCache的database需要遵循KeyedCacheSupporting
于是就有了FluentSQLiteProvider文件中的
public typealias SQLiteCache = DatabaseKeyedCache<ConfiguredDatabase<SQLiteDatabase>> extension SQLiteDatabase: KeyedCacheSupporting { }
至此我们大致就了解db最为cache需要的条件了,之余具体实现我们不深究啦。
2.2 配置Model
db作为cache那一定需要的是Enty来实现啦,我们看看怎么来做
刚开始的时候我也是不知到这个名称系统是怎么玩的,于是猜测肯定是个Cache enty的结合体,于是就试了一下输入Cache编辑器提示了个CacheEntry于是点击去看了类型定义
Fluent model powering default implementation for KeyedCacheSupporting where self is QuerySupporting. You can use this type to interact with the cache table if it has been configured. public final class CacheEntry<Database>: Model where Database: QuerySupporting
看来猜测还是挺准确的 ?
接下来就是老套路啦
migrations.add(model: CacheEntry<SQLiteDatabase>.self, database: IADatabaseIdentifier)
3 Redis
config.prefer(RedisCache.self, for: Cache.self)