Пример #1
0
        public async Task <string> GetLaGouToken(string city, string key)
        {
            var cacheKey  = "拉勾" + city + key;
            var time      = DateTime.Now.AddMinutes(10) - DateTime.Now;//缓存10分钟
            var easyCache = new EasyCache <string>(cacheKey, time);
            var cacheData = easyCache.GetData();

            if (cacheData != null)
            {
                return(cacheData);
            }

            var searchUrl = $@"https://www.lagou.com/jobs/list_{key}?px=new&city={city}#order";

            using var client = new HttpClient();
            var httpResponseMessage = await client.GetAsync(searchUrl);

            var cookies = httpResponseMessage.Headers.GetValues("Set-Cookie");
            var sb      = new StringBuilder();

            foreach (var cookie in cookies)
            {
                var split = cookie.Split(";");
                if (split.Length <= 0)
                {
                    continue;
                }
                sb.Append(split[0]);
                sb.Append(";");
            }
            var token = sb.ToString();

            easyCache.AddData(token);
            return(token);
        }
Пример #2
0
        public void Test_string()
        {
            var time = DateTime.Now.AddMilliseconds(60) - DateTime.Now;
            EasyCache <string> str = new EasyCache <string>("k", time);

            str.AddData("aaa");
            Assert.Equal(str.GetData(), "aaa");
        }
Пример #3
0
        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <param name="city"></param>
        /// <param name="key"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public EasyCache <List <JobInfo> > GetCacheObject(int?minutes = null)
        {
            var key  = Request.Path.Value + Request.QueryString.Value;
            var time = DateTime.Now.AddMinutes(minutes ?? 10) - DateTime.Now;//缓存10分钟
            EasyCache <List <JobInfo> > obj = new EasyCache <List <JobInfo> >(key, time);

            return(obj);
        }
Пример #4
0
        public void Test_Obj()
        {
            var time = DateTime.Now.AddMilliseconds(60) - DateTime.Now;
            EasyCache <Order> obj = new EasyCache <Order>("k", time);

            obj.AddData(new Order()
            {
                Code = "123"
            });
            Assert.Equal(obj.GetData()?.Code, "123");
        }
Пример #5
0
 const string SPACE = "!";       //最好不要设置变量名可以使用的字符
 static EasyReflect()            //静态构造方法,设置缓存区大小
 {
     cacheGet  = new EasyCache <ReflectGet>(25);
     cacheSet  = new EasyCache <ReflectSet>(25);
     cacheCall = new EasyCache <ReflectCall>(25);
 }