Exemplo n.º 1
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();
            }
        }
Exemplo n.º 2
0
        public static List <T> Get <T>(string key)
        {
            mutex.WaitOne();

            try
            {
                using (var db = new SqliteContext(dataSource))
                {
                    var record = db.SqliteFixedSet.FirstOrDefault(p => p.Key == key);
                    return(record == null ? new List <T>() : JsonConvert.DeserializeObject <List <T> >(record.Value));
                }
            }
            catch (Exception e)
            {
                RuntimeLogger.Log(e);
                return(new List <T>());
            }
            finally
            {
                mutex.ReleaseMutex();
            }
        }