示例#1
0
        public void Init()
        {
            if (Settings == null)
            {
                Settings = new Dictionary <string, string>();
            }

            try
            {
                if (!File.Exists(Assembly.GetExecutingAssembly().Location.Replace(".dll", ".json")))
                {
                    File.WriteAllText(Assembly.GetExecutingAssembly().Location.Replace(".dll", ".json"), Properties.Resources.Plugin_SQLCache);
                }

                if (File.Exists(Assembly.GetExecutingAssembly().Location.Replace(".dll", ".json")))
                {
                    JConfig             = JObject.Parse(File.ReadAllText(Assembly.GetExecutingAssembly().Location.Replace(".dll", ".json")));
                    bReadOnly           = JConfig["ReadOnly"].Value <bool>();
                    ContinueAfterWrite  = JConfig["ContinueAfterWrite"].Value <bool>();
                    CacheFull           = JConfig["CacheFull"].Value <bool>();
                    CacheKeys           = JConfig["CacheKeys"].Value <bool>();
                    SQLConnectionString = JConfig["SQLConnectionString"].Value <string>();
                    SQLTable            = JConfig["SQLTable"].Value <string>();
                    SlidingExpiration   = JConfig["SlidingExpiration"].Value <int>();
                }
                else
                {
                    JConfig = new JObject();
                }

                try
                {
                    SqlServerCacheOptions oOption = new SqlServerCacheOptions()
                    {
                        ConnectionString         = SQLConnectionString,
                        SchemaName               = "dbo",
                        TableName                = "JCache",
                        DefaultSlidingExpiration = new TimeSpan(0, 0, 0, SlidingExpiration)
                    };

                    oSrv = new SqlServerCache(oOption);
                    oSrv.SetString("key1", "value1", new DistributedCacheEntryOptions()
                    {
                        SlidingExpiration = new TimeSpan(1000)
                    });
                }
                catch
                {
                    try
                    {
                        using (SqlConnection connection = new SqlConnection(SQLConnectionString))
                        {
                            SqlCommand command = new SqlCommand(Properties.Resources.CreateTable, connection);
                            command.Connection.Open();
                            command.ExecuteNonQuery();
                        }

                        SqlServerCacheOptions oOption = new SqlServerCacheOptions()
                        {
                            ConnectionString         = SQLConnectionString,
                            SchemaName               = "dbo",
                            TableName                = "JCache",
                            DefaultSlidingExpiration = new TimeSpan(0, 0, 0, SlidingExpiration)
                        };

                        oSrv = new SqlServerCache(oOption);
                        Console.WriteLine("SQL Table 'JCache' created...");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: " + ex.Message);
                    }
                }

                oSrv.SetString("key1", "value1", new DistributedCacheEntryOptions()
                {
                    SlidingExpiration = new TimeSpan(1000)
                });
                Console.WriteLine(oSrv.GetString("key1"));
            }
            catch { }
        }
示例#2
0
        public bool WriteHash(string Hash, string Data, string Collection)
        {
            if (bReadOnly)
            {
                return(false);
            }

            if (string.IsNullOrEmpty(Data) || Data == "null")
            {
                return(true);
            }

            Collection = Collection.ToLower();

            if (Collection == "_full")
            {
                if (!CacheFull) //exit if ChacheFull is not set
                {
                    if (ContinueAfterWrite)
                    {
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }

                if (CacheKeys)
                {
                    var jObj = JObject.Parse(Data);
                    jaindb.jDB.JSort(jObj);

                    string sID = jObj["#id"].ToString();

                    //Store KeyNames
                    foreach (JProperty oSub in jObj.Properties())
                    {
                        if (oSub.Name.StartsWith("#"))
                        {
                            if (oSub.Value.Type == JTokenType.Array)
                            {
                                foreach (var oSubSub in oSub.Values())
                                {
                                    try
                                    {
                                        if (oSubSub.ToString() != sID)
                                        {
                                            WriteLookupID(oSub.Name.ToLower(), (string)oSub.Value, sID);
                                        }
                                    }
                                    catch { }
                                }
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty((string)oSub.Value))
                                {
                                    if (oSub.Value.ToString() != sID)
                                    {
                                        try
                                        {
                                            WriteLookupID(oSub.Name.ToLower(), (string)oSub.Value, sID);
                                        }
                                        catch { }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //Cache Data
            if (SlidingExpiration >= 0)
            {
                oSrv.SetString(Collection + "\\" + Hash, Data, new DistributedCacheEntryOptions()
                {
                    SlidingExpiration = new TimeSpan(0, 0, SlidingExpiration)
                });
            }
            else
            {
                oSrv.SetString(Collection + "\\" + Hash, Data, new DistributedCacheEntryOptions()
                {
                    SlidingExpiration = null
                });
            }

            if (ContinueAfterWrite)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }