/// <summary> /// HttpCache表中获取或创建 /// </summary> /// <param name="key"></param> /// <param name="expireTime"></param> /// <param name="factory"></param> /// <returns></returns> public async Task <byte[]> GetOrCreateHttpCacheAsync(string key, TimeSpan expireTime, Func <Task <byte[]> > factory) { var a = _con.HttpClientCache.Where(e => e.Key.Equals(key)); if (await a.CountAsync() > 0) { var b = await a.FirstOrDefaultAsync(); if (DateTimeOffset.UtcNow.ToUnixTimeSeconds() - b.TimeStamp > expireTime.TotalSeconds || b.Value.Length == 0) { var c = await factory(); b.Value = c; b.TimeStamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); _con.HttpClientCache.Update(b); await _con.SaveChangesAsync(); } return(b.Value); } var d = new HttpClientCacheTable { Key = key, Value = await factory() }; await _con.HttpClientCache.AddAsync(d); await _con.SaveChangesAsync(); return(d.Value); }
/// <summary> /// 插入数据 /// </summary> /// <param name="video"></param> /// <returns>结果</returns> public async Task <VideoTable> InsertAsync(VideoTable video) { var a = await _con.Video.AddAsync(video); if (await _con.SaveChangesAsync() > 0) { return(a.Entity); } return(null); }
/// <summary> /// 创建用户 /// </summary> /// <param name="name"></param> /// <param name="password"></param> /// <param name="phoneNumber"></param> /// <param name="email"></param> /// <param name="role"></param> /// <returns></returns> public async Task <bool> InsertAsync(string name, string password, string phoneNumber = "", string email = "", UserRole role = UserRole.GeneralUser) { if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(password)) { return(false); } var salt = new RandomStringBuilder().Create(6); var user = new UserTable { Name = name, Email = email, PhoneNumber = phoneNumber, Salt = salt, PassWord = Md5.GetMd5(password, salt), Role = role }; await _con.User.AddAsync(user); return(await _con.SaveChangesAsync() > 0); }
/// <summary> /// 插入弹幕 /// </summary> /// <param name="danmu">弹幕信息</param> /// <returns>是否成功</returns> public async Task <bool> InsertDanmuAsync(DanmuTable danmu) { await _con.Danmu.AddAsync(danmu); return(await _con.SaveChangesAsync() > 0); }