Exemplo n.º 1
0
        public static void Cache <T>(string key, T data, DateTime expTime)
        {
            mutex.WaitOne();
            try
            {
                using var db = new SqliteContext(dataSource);
                var query = db.SqliteCacheModel.FirstOrDefault(m => m.Key == key);
                if (query == null)
                {
                    db.SqliteCacheModel.Add(new SqliteCacheModel
                    {
                        Key = key, Value = JsonConvert.SerializeObject(data), ExpTime = expTime.ToString(CultureInfo.CurrentCulture)
                    });
                }
                else
                {
                    query.Value   = JsonConvert.SerializeObject(data);
                    query.ExpTime = expTime.ToString(CultureInfo.CurrentCulture);
                }

                db.SaveChanges();
            }
            catch (Exception e)
            {
                RuntimeLogger.Log(e);
            }
            finally
            {
                mutex.ReleaseMutex();
            }
        }
Exemplo n.º 2
0
        public static void SetMaxCount(string key, int count)
        {
            mutex.WaitOne();

            try
            {
                using var db = new SqliteContext(dataSource);
                var record = db.SqliteFixedSet.FirstOrDefault(p => p.Key == key);
                if (record == null)
                {
                    record = new SqliteFixedSet()
                    {
                        Key = key, MaxCount = count, Value = "[]"
                    };

                    db.SqliteFixedSet.Add(record);
                }
                else
                {
                    record.MaxCount = count;
                }

                db.SaveChanges();
            }
            catch (Exception e)
            {
                RuntimeLogger.Log(e);
            }
            finally
            {
                mutex.ReleaseMutex();
            }
        }
Exemplo n.º 3
0
        public static void Cache <T>(string key, T data)
        {
            mutex.WaitOne();

            try
            {
                using (var db = new SqliteContext(dataSource))
                {
                    var record = db.SqliteFixedSet.FirstOrDefault(p => p.Key == key);
                    if (record == null)
                    {
                        record = new SqliteFixedSet()
                        {
                            Key   = key,
                            Value = JsonConvert.SerializeObject(new List <T>()
                            {
                                data
                            }),
                            MaxCount = 10
                        };

                        db.SqliteFixedSet.Add(record);
                    }
                    else
                    {
                        var list = JsonConvert.DeserializeObject <List <T> >(record.Value);
                        list         = list.Prepend(data).Take(record.MaxCount).ToList();
                        record.Value = JsonConvert.SerializeObject(list);
                    }

                    db.SaveChanges();
                }
            }
            catch (Exception e)
            {
                RuntimeLogger.Log(e);
            }
            finally
            {
                mutex.ReleaseMutex();
            }
        }