Hello! I am new to c #, but I know java well.

The question that I am asking now should be very easy: how to cache and retrieve from the cache?

For example, when you contact some Internet address, first search in the cache, if there is no cache, then you get this content from the Internet and put it in the cache

If you know C # and Java, below I gave Java code
You can answer this question without knowing Java.

Main.java

import org.apache.commons.io.IOUtils; import java.io.IOException; import java.io.InputStream; import java.net.*; import java.util.List; import java.util.Map; public class Main { public static void main(String[] args) throws Exception { ResponseCache.setDefault(new ResponseCache() { @Override public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException { if (uri.toString().equals("http://google.com/")) { return new CacheResponse() { @Override public Map<String, List<String>> getHeaders() throws IOException { return null; } @Override public InputStream getBody() throws IOException { return null; } }; } return null; } @Override public CacheRequest put(URI uri, URLConnection conn) throws IOException { return null; } }); URLConnection c = new URL("http://google.com/").openConnection(); c.setUseCaches(true); String html = IOUtils.toString(c.getInputStream()); System.out.println(html); } } 

Please provide an example of C # code by analogy with my code.

  • See Cache - used in web applications, MemoryCache - used in desktop applications. - Alexander Petrov
  • Already rejoice: / ... Who do you contact? Specify @ nickname. Here is the address where most questions will be answered. - Alexander Petrov

1 answer 1

Let Response GetWebResponseFromUrl(string url) - some method (which you write yourself) to get Web-content, then the answer to your question looks like this:

 Dictionary<string, Response> ResponseCache = new Dictionary<string, Response>(); Response GetWebResponse(string url) { if (!ResponseCache.ContainsKey(url)) ResponseCache[url] = GetWebResponseFromUrl(url); return ResponseCache[url]; } 
  • I almost understood you can give an example in detail - Bahodir