I need to load an xml file into memory and have it available for globally for the whole site. Does this code accomplish this?
If so, how is updating this "cached" version accomplished in the future?
XPathDocument ConvProductDoc;
ConvProductDoc = Cache["doc"] as XPathDocument;
if (ConvProductDoc == null) {
ConvProductDoc = new XPathDocument(HttpContext.Current.Request.MapPath(#"\data\foo\bar\my.xml"));
Cache.Insert("doc", ConvProductDoc);
}
Yes, the ASP.NET Cache object is site wide. There are a lot of options for managing the Cache and setting expiration rules, etc.
To assign/update the value in Cache, you simply set it like you would any Dictionary or HashTable value:
Cache["doc"] = newValue;
You can read a lot more about the Cache object in the MSDN Docs here: http://msdn.microsoft.com/en-us/library/aa478965.aspx#aspnet-cachingtechniquesbestpract_topic4
If you website is only on one server then yes.
If your website is distributed over more than one server then no.
The cache key and data will only be available on the server in which it has been stored.
If you server is in Amazon EC2 for example, you could use ElastiCache which would ensure the cache is available over a distributed server environment.
Related
I have sample webapp deployed to Azure. The app cached a variable using MemoryCacheEntryOptions to store a value (from database) which expire in 5 minutes.
However after 5 minutes via Chrome debugging tool, I still can query the cache, the cache value expected to be empty or whatever the new value which currently stored updated in the database.
I even tried to clear cache in the web browser, but cache seem still retain the previous value.
However when I restart the web site, and open the web app again the cache value is no longer exist.
Would any setting in Azure might affect the cache expiry?
private readonly MemoryCacheEntryOptions _cacheEntryOptions;
protected CacheService(IMemoryCache memoryCache)
{
_ memoryCache = memoryCache;
_cacheEntryOptions = new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(300)
};
}
Debugging the behavior of a web application is notoriously hard, as all you got to control it is the Browser - and you never get exclusive access.
Even if you did not refresh the page, any number of things might have queried the server. The culprits start around "any search engines webcrawler" and end around "somewhat aggressive security tools" (because some viruses might use web servers). You could try a way shorter timeout. But ideally you want to have both the Server and the client you access it with run in separate virtual machines, which are only connected via the Hypervisor. That way you can be certain nobody is interfering.
I have a Webapp for different customers-DBs which runs on several Application Servers. The customers are each assigned to an instance on a AS.
For several of the customers, certain data is saved in additional SQLite-DBs on the Application Servers themselves; when this kind of data is added, the WebApp tests whether the according SQLite-DB already exists on this AS and if not, it creates it by using the following code:
dbFileName = "C:\\" + dbFileName;
SQLiteConnection.CreateFile(dbFileName);
using (System.Data.SQLite.SQLiteConnection con = new System.Data.SQLite.SQLiteConnection("data source=" + dbFileName))
{
using (System.Data.SQLite.SQLiteCommand com = new System.Data.SQLite.SQLiteCommand(con))
{
con.Open();
The problem is that if I assign the customer to an instance on another AS, the SQLite-db has to be created again, since it can't access the one on the other AS.
Now my idea was to create the SQLite-dbs on some azure storage, where I could access it from every AS, but so far I'm not able to access it via a SQLite-Connection.
I have knowledge of my specific SAS (=Shared Access Signature) and Connectionstrings like the ones specified on https://www.connectionstrings.com/windows-azure/
but I'm not sure which part I should use for the SQLiteConnection.
Is it even possible?
The only examples on connections to Azurestorage that I found so far are via HttpRequests (like How to access Azure blob using SAS in C#) which doesn't help me or can anybody show me a way to use this for my problem?
Please tell me if you need more information, I'm kind of bad at explaining things and not taking into account that many things aren't common knowledge...
You can not use Azure storage blob as a normal file system. The data source in SQLite connection string should be a file path or memory.
If you want to use Azure storage blob, as far as I know, you can only mount Blob storage as a file system with blobfuse on Linux OS. But this is not 100% compatible with normal file systems.
Another choice is to use Azure storage file, it supports SMB protocol. You can mount a network driver and use it.
I am working on a requirement to cache some database values, that can be reused. But I want the cache to be accessible to all the processor in the same server.
Overview:
So basically, there will be multiple processors that get work from an API and process the record to the database. Some of these database values will be cached.
The processors will be multiple windows services and I want them to share the same cache.
How can this be achieved using Ncache. I am pretty new to using this. So any links or directions are greatly appreciated.
The biggest value to NCache is that is can be used as an OutProc distributed in-memory cache, where the cache resides within the NCache process itself; this differs from an InProc cache where access would be limited to a single process.
You need to configure an OutProc cache running on either a separate dedicated caching server (or cluster) or on the same server as your services.
Refer to http://www.alachisoft.com/resources/docs/ncache/admin-guide/local-cache.html for more information on OutProc and InProc caches.
Once you install the NCache server, you can create your caching configuration by modifying the config.ncconf file that by default lives at C:\Program Files\NCache\config.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration>
<cache-config cache-name="MyOutProcCacheName">
<cache-settings inproc="False">
<logging enable-logs="True" trace-errors="True" trace-debug="False" log-path=""/>
<performance-counters enable-counters="True" snmp-port="0"/>
<cache-notifications item-remove="False" item-add="False" item-update="False"/>
<cleanup interval="15sec"/>
<storage type="heap" cache-size="2024mb"/>
<eviction-policy default-priority="normal" eviction-ratio="5%"/>
<cache-topology topology="local-cache"/>
<client-death-detection enable="False" grace-interval="60sec"/>
</cache-settings>
</cache-config>
</configuration>
The above configuration will create an OutProc cache on the local server (see cache-topology). This can be configured as a various mirrored, partitioned, or replicated caches in a clustered environment if needed (refer to http://www.alachisoft.com/resources/docs/ncache/admin-guide/cache-topologies.html).
You can then start the NCache service, and connect to the service from within your application, and initialize connection to the named cache instance in the configuration above.
Cache outProcCache = NCache.InitializeCache("MyOutProcCacheName");
You can also configure the connection to the NCache server/service entirely within the code instead of a client.ncconf file by sending configuration parameters to the InitializeCache method above.
CacheInitParams connectionParams = new CacheInitParams();
connectionParams.ServerList = new CacheServerInfo[]{ new CacheServerInfo("ncacheIp", ncachePort) };
Cache outProcCache = NCache.InitializeCache("MyOutProcCacheName", connectionParams);
I have a simple web app built in asp.net webforms c#
where and how would be the best way to save info from the code behind?
(and also retrieve that info)
eg all i want to save is a dateTime. and a flag set to True or False.
and be able to access them in the code behind.
Im not using a db for this web app.
Edit: and can't really use session variables for this purpose.
thanks
If you have to save data for an arbitrary period of time, then you need to store it in a database.
If you need to read the saved variable after a lengthy period of time, you should store it in a local file or a database.
You can create a file using a FileStream object and then write your value to the file.
To get a path where your application has sufficient rights to write a file, use Server.MapPath.
Note : If possible, and if the data you store should not be available to users, you should configure your IIS WebSite to forbid him to serve this file to clients.
Use Application variable like Application["thedate"] = date; and you can get back as date = Application["thedate"].
The date will be saved untill app pool restarts (which also happens when IIS or system restarts).
For a more longer time, save in an xml file on the disk (Use XMLReader and XMLWriter for this purpose).
If this is per user info you could use either browser cookie or viewstate.
Use the Session object:
Session["thedate"] = date;
DateTime date = (DateTime)Session["thedate"];
I would like to know in how the Session locking mechanism work and how I can lock a variable and its respective child objects for multiple reads/exclusive write in a server farm environment.
Scenario
The web farm will use 3 Windows 2003 servers, each server as its own app domain for the Web application. The sesion object is saved on SQL Server 2005.
The object to use in my web app is at follows:
MySampleClass = class
{
public string Id;
public Dictionary<string, CustomClass1> Data;
public List<string> Commands;
public CustomClass2 MoreData;
}
where customClass 1 and 2 are business classes that are part of the application.
now in one of the web pages, code will look like:
Session["myObj"] = new MySampleClass();
in other pages:
MySampleClass = (MySampleClass)Session["myObj"];
//Is Session["myObj"] accessed in a multiple reader/exclusive writer mode? if so is it locking just the variable or the whole contents?
MySampleClass.Commands.Add("sample string");
MySampleClass.Commands.RemoveAt(0);
//More CRUD changes
//Are these changes available to other pages as soon as I finish the CRUD changes?
let me know if you need more details
Have a look here under Locking Session-Store Data. Basically, unless your page says it wants read-only session access, the session is locked on the DB and other callers for that session will poll at 1/2sec interval until it is unlocked.
There is also a detailed explanation on "Session State Providers" on msdn.
It covers the algorithm and rules used when using out-of-process session state providers in ASP.NET.