SwiftStrideIO

一个 Swift 库,用于轻松缓存和检索来自 URL 的数据,具有自动缓存功能并支持自定义密钥加密策略。

特性

安装

Swift Package Manager (SPM) 使用 Xcode

要使用 Swift Package Manager 将 SwiftStrideIO 集成到您的 Xcode 项目中,请按照以下步骤操作

  1. 在 Xcode 中打开您的项目。
  2. 导航到菜单栏并单击 File > Swift Packages > Add Package Dependency...
  3. 在新出现的窗口的搜索栏中,粘贴以下 URL:https://github.com/rayhaanalykhan/SwiftStrideIO.git
  4. 按照屏幕上的说明选择软件包选项和您要集成的版本。
  5. 完成后,Xcode 将下载软件包并将其添加到您的项目导航器中。

通过按照上述说明安装 SwiftStrideIO,您还将自动解析此软件包使用的 CipherEncryption 依赖项。

用法

要在您的项目中使用 SwiftStrideIO,您可以直接调用其静态方法

  1. 缓存数据

    要从 URL 缓存数据,请使用 cacheData 方法

    // replace Data() with your actual Data
    SwiftStrideIO.cacheData(data: Data(), url: URL(string: "https://www.someurl.com/dummy")!)

    如果需要,处理缓存 URL

    // replace Data() with your actual Data
    SwiftStrideIO.cacheData(data: Data(), url: URL(string: "https://www.someurl.com/dummy")!) { cacheUrl in }

    使用指定的加密密钥(默认为 SHA1)的用法

    // Replace Data() with your actual data
    SwiftStrideIO.cacheData(data: Data(), url: URL(string: "https://www.someurl.com/dummy")!, keyEncryption: .SHA224) { cacheUrl in }

    类似地使用您自己的密钥缓存数据

    // replace Data() with your actual Data
    SwiftStrideIO.cacheData(data: Data(), cacheKey: "someKey")

    如果需要,处理缓存 URL

    // replace Data() with your actual Data
    SwiftStrideIO.cacheData(data: Data(), cacheKey: "someKey") { cacheUrl in }
  2. 检索缓存的数据

    要从缓存中检索数据,请使用 getCachedData 方法

    SwiftStrideIO.getCachedData(from: URL(string: "https://www.someurl.com/dummy")!) { (data, cacheUrl) in
        // Use the retrieved data from cache or use/pass cache url
    }

    使用指定的加密密钥(默认为 SHA1)的用法

    SwiftStrideIO.getCachedData(from: URL(string: "https://www.someurl.com/dummy")!, keyEncryption: .SHA224) { cacheUrl in
        // Use the retrieved data from cache or use/pass cache url
    }

    或使用缓存密钥来检索数据

    SwiftStrideIO.getCachedData(cacheKey: "someKey") { (data, cacheUrl) in
        // Use the retrieved data from cache or use/pass cache url
    }
  3. 获取数据

    要从本地 URL 检索数据或从服务器 URL 检索和缓存数据,请使用 getData 方法

    SwiftStrideIO.getData(from: URL(string: "https://www.someurl.com/dummy")!) { (data, localUrl) in
        // Process the fetched data
    }

    使用指定的加密密钥(默认为 SHA1)的用法

    SwiftStrideIO.getData(from: URL(string: "https://www.someurl.com/dummy")!, keyEncryption: .SHA224) { (data, localUrl) in
        // Process the fetched data
    }

    或者,您可以使用带有可选基本 URL 字符串参数的字符串 URL

    SwiftStrideIO.getData(with: "path/or/url", baseUrlString: "optional/baseUrl") { (data, localUrl) in
        // Process the fetched data
    }

    使用指定的加密密钥(默认为 SHA1)的用法

    SwiftStrideIO.getData(with: "path/or/url", baseUrlString: "optional/baseUrl", keyEncryption: .SHA224) { cacheUrl in
        // Use the retrieved data from cache or use/pass cache url
    }
  4. 清除缓存

    要清除所有缓存文件,请使用 clearCache 方法

    SwiftStrideIO.clearCache()

许可证

SwiftStrideIO 根据 MIT 许可证发布。有关更多详细信息,请参阅 LICENSE 文件。

贡献

欢迎贡献者 fork 该项目并提交 pull request。如果可能,请为任何新的或现有功能包含单元测试。此外,请相应地更新 README。

联系方式

如需更多信息,请通过电子邮件 rayhaanalykhan@gmail.com 联系我。