NScript.Storage 8.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package NScript.Storage --version 8.0.1                
NuGet\Install-Package NScript.Storage -Version 8.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NScript.Storage" Version="8.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NScript.Storage --version 8.0.1                
#r "nuget: NScript.Storage, 8.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NScript.Storage as a Cake Addin
#addin nuget:?package=NScript.Storage&version=8.0.1

// Install NScript.Storage as a Cake Tool
#tool nuget:?package=NScript.Storage&version=8.0.1                

NScript.Storage

NScript.Storage 项目,针对 AI 应用场景,基于 RocksDB,提供了一些方便的文件存储的功能。当前支持:

  • 分片式文件存储

分片式文件存储

LocalShardingFileStorageService 类支持按照年或月或日,进行分片式存储。可以设定一个文件大小的阈值,当文件大小小于该阈值时,将文件存储在 RocksDB 中,当文件大小大于该阈值时,将文件存储在磁盘上。可以根据文件 id 进行读取和删除。

需要注意的是,分片式文件存储规定文件 id 必须为特殊格式,规定如下:

  • 按年分片:文件 id 示例为 20240000*************
  • 按月分片:文件 id 示例为 20241200*************
  • 按日分片:文件 id 示例为 20241221*************

文件 id 的前8个字符为分片/分桶的id,有时也称为 bucket id。

ShardingStrategy 提供了扩展方法 GetShardingId 可以获得某 DateTime 的分片id。提供了 NextFileId 扩展方法,可以直接获得某 DateTime 下的合法随机文件 id。

使用示例:


    var dir = new DirectoryInfo(DateTime.Now.ToFileTime().ToString());

    var shardingStrategy = ShardingStrategy.ByDay;
    var maxMBytesSaveInRocksDB = 16;    // 16M 以下的存在 rocksdb 中
    var maxRocksDBCacheBuckets = 2;     // rocksdb 的分片实例缓存数量,避免频繁打开关闭 rocksdb
    var service = new LocalShardingFileStorageService(shardingStrategy, maxMBytesSaveInRocksDB, maxRocksDBCacheBuckets, dir.FullName);
    var bytes1 = new byte[1024];
    bytes1[0] = 1;
    var bytes2 = new byte[1024 * 1024 * 24];
    bytes2[0] = 2;
    var bytes3 = new byte[1024];
    bytes3[0] = 3;

    // 存储前先获取 fileId。根据当前时间,计算分片id,分配对应的随机文件名
    var fileId1 = service.NextFileId(".dat");

    service.Save(fileId1, bytes1);

    // 直接存储,返回根据当前时间,计算分片id,分配对应的随机文件名
    var fileId2 = service.Save(bytes2, ".dat");

    service.Save(fileId2, bytes2);

    // 根据指定时间和分片策略,计算分片 id,分配随机文件名
    var fileId3 = shardingStrategy.NextFileId(new DateTime(2024, 12, 20), ".dat");

    service.Save(fileId3, bytes3);

    // 查找
    bytes1 = service.Find(fileId1);
    bytes2 = service.Find(fileId2);
    bytes3 = service.Find(fileId3);

    // 删除文件
    bool deleteResult = service.Delete(fileId1);

Save 方法也支持对 Stream 进行操作。另提供了 FindStream 方法,以流的方式获取文件。对于大文件的处理,推荐通过流的方式进行。

RocksDBShardingFileStorageService类和 LocalDiskShardingFileStorageService类也提供和 LocalShardingFileStorageService 相似的服务,不同的是,RocksDBShardingFileStorageService 的数据只存于 RocksDB 中,LocalDiskShardingFileStorageService 的数据只存于文件系统中。

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on NScript.Storage:

Package Downloads
NScript.Storage.LiteDB

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.1.1 40 1/12/2025
8.0.1 54 1/10/2025