Is there any impact on two websites with the same cache name running on the same server(Cache scope)

Lionsure 2020-08-21 Original by the website

In order to save server resources and speed up the display of web pages, some data with high real-time requirements are often cached. If two websites use the same cache name and run them on the same server, will its values overwrite each other? If it does, the program will get the wrong data. We know that variables and methods have a scope of action, and cache also has a scope of action. In theory, even if the cache name of different websites is the same, deploy it to the same server. Since each has its own scope of action, they will not interact with each other. Naturally, the wrong data will not be got.

In order to confirm whether two websites with the same cache name are running on the same server, whether it has any effect, let us give a specific example. First add a cache named CacehTest to a website A, and then add a cache named CacehTest to another website B, and set different data for them.

 

Examples of two websites with the same cache name run on the same server(Cache scope)

1. Add a cache named CacehTest to website A

private void GetCacheName()
       {
              string cacheName = "CacehTest";
              if (Cache[cacheName] == null)
                     Cache.Insert(cacheName, "Cache1", null, Cache.NoAbsoluteExpiration, TimeSpan.FromHours(10), CacheItemPriority.Normal, null);

       lbl1.Text = Cache[cacheName].ToString();
       }

When this method is called when the page loads, the cached values will be output on the foreground page. The cached value is assigned to a Label control, and the final output value is Cache1. In order to further verify whether the wrong data is got, the second time the page of website B is executed and then the page of website A is executed, and the result is also output Cache1, indicating that no wrong data is got, and the scope of Cache is limited to the website where it is located.

 

 

2. Add a cache named CacehTest on website B

private void GetCacheName()
       {
              string cacheName = "CacehTest";
              if (Cache[cacheName] == null)
                     Cache.Insert(cacheName, "Cache2", null, Cache.NoAbsoluteExpiration, TimeSpan.FromHours(10), CacheItemPriority.Normal, null);

       lbl2.Text = Cache[cacheName].ToString();
       }

Save a value of Cache2 in the Cache of website B, and assign this value to a Label control named lbl2, and the final output value is Cache2. Then execute the page of website A once and then execute the page of website B. The result is also output to Cache2, indicating that no wrong data is got.