OpenAuth.Net/docs/core/cache.md
2023-08-11 17:47:02 +08:00

62 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 缓存机制
## 服务器缓存
在OpenAuth中缓存通过`ICacheContext`接口实现的。系统有三个实现方式:
* 基于.net自带的`MemoryCache`实现的`CacheContext`
* 基于`StackExchange.Redis`实现的`RedisCacheContext`【新增💪】
* 基于`Enyim Memcache`实现的`EnyimMemcachedContext`
可以根据自己需要扩展其他缓存。OpenAuth.Mvc和OpenAuth.Api默认使用的是CacheContext。可以在`AutofacExt.cs`跟换自己喜欢的缓存:
```csharp
public static void InitAutofac(ContainerBuilder builder)
{
...
//更换缓存
builder.RegisterType(typeof(CacheContext)).As(typeof(ICacheContext));
}
```
## 服务器缓存有效时间
服务器默认缓存时间为10天在`LoginParse.cs`中设置
```csharp
_cacheContext.Set(currentSession.Token, currentSession, DateTime.Now.AddDays(10));
```
::: warning 注意事项1
默认使用的是.net的内存Cache在用IIS发布后由于IIS本身存在自动回收的机制会导致系统缓存20分钟就会失效。
:::
::: warning 注意事项2
如果使用Redis缓存注意调整配置文件中关于redis的配置
```csharp
"AppSetting": {
//其他配置..
"RedisConf": "your_redis_server:6379,password=your_redis_password" //redis配置信息
}
```
:::
## 企业版前端缓存
企业版前端使用js-cookie存储登录token信息。默认为`session cookie`也就是这个session在关闭浏览器后会被删除。如果想延长登录有效期限可以调整`src\utils\auth.js`中写入cookie的时间
```javascript
//设置登录有效期为7天
export function setToken(token) {
return Cookies.set(TokenKey, token, { expires: 7 })
}
```