储存
为插件提供便捷的字符串储存功能
类型声明
- C#
- JavaScript
namespace Serein.Core.Services.Plugins.Storages;
public partial class StorageBase
{
public int Length { get; }
public void Clear();
public string? GetItem(string key);
public void RemoveItem(string key);
public void SetItem(string key, string value);
public string? Key(int index);
public string? this[string key] { get; set; }
}
/**
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage)
*/
interface Storage {
/**
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/length)
*/
readonly length: number;
/**
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/clear)
*/
clear(): void;
/**
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/getItem)
*/
getItem(key: string): string | null;
/**
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/key)
*/
key(index: number): string | null;
/**
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/removeItem)
*/
removeItem(key: string): void;
/**
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Storage/setItem)
*/
setItem(key: string, value: string): void;
[name: string]: any;
}
本地储存
保存在本地文件(Serein/plugins/local-storage.json
)中
提示
你可以借助此功能实现一些简单的配 置文件的储存
- C#
- JavaScript
示例
var localStorage = serviceProvider.GetRequiredService<LocalStorage>();
localStorage.SetItem("key", "value");
declare var localStorage: Storage;
与浏览器中的Window.localStorage
一致
会话储存
仅保存在内存中,退出后数据丢失
提示
通常用于储存在同一个Serein生命周期内可以重复利用的数据
- C#
- JavaScript
示例
var sessionStorage = serviceProvider.GetRequiredService<SessionStorage>();
sessionStorage["key"] = "value";
declare var sessionStorage: Storage;
与浏览器中的Window.sessionStorage
一致