예제 #1
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();
            }
        }
예제 #2
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();
            }
        }