1024programmer Asp.Net CefSharp custom cache implementation

CefSharp custom cache implementation

CefSharp custom cache implementation

Hello everyone, I am the wolf at the end of the desert.

The above introduces “C# uses CefSharp to embed web pages – and gives examples of interaction between C# and JS”. This article introduces the cache implementation of CefSharp. First, let’s talk about the benefits of adding cache:

  1. Improve page loading acceleration: CefSharp cache can cache already loaded pages and resources. When users visit the same page again, they can be loaded directly from the cache without re-downloading and parsing the page and resources, thus speeding up the page. Loading speed.
  2. Reduce network traffic: Using cache can reduce network traffic, because already downloaded resources can be read directly from the cache without re-downloading.
  3. Improve user experience: Because caching can increase page loading speed, it can improve the user’s experience. Users can access pages and resources faster, making it more enjoyable to use the application.
  4. Reduce server load: Using cache can reduce server load, because already downloaded resources can be read directly from the cache without the need to regenerate and send them.
  5. Offline access: The application can be enabled to support offline access because it can cache downloaded pages and resources. When the user does not have a network connection, the pages and resources can be loaded directly from the cache.

In short, using cache can improve application performance and user experience, reduce network traffic and server load, and support offline access, which is a very useful feature.

Example of this article: Github

When the network is disconnected, the demo loads the cached 4 pages of Baidu, Baidu Translate, Dotnet9 Home Page, and Dotnet9 About:

The following explains how caching is implemented.

1. Default cache implementation

CefSharp’s default caching implementation is based on Chromium’s caching mechanism. Chromium uses two types of cache: memory cache and disk cache.

1.1. Memory cache

The memory cache is a cache based on the LRU (least recently used) algorithm, which caches recently accessed pages and resources. The size of the memory cache is limited, and when the cache reaches the maximum size, the least recently used pages and resources are removed.

Memory caching cannot be set via CefSharp.WPF’s API. Specifically, Chromium maintains an LRU (Least Recently Used) cache in memory to store recently visited web page data. When cache space is insufficient, Chromium will automatically clear the least recently used cache data based on the LRU algorithm to make room for new data.

In CefSharp.WPF, we can clear the data in the memory cache by calling the Cef.GetGlobalRequestContext().ClearCacheAsync() method. This method clears all cached data, including memory cache and disk cache. If you only need to clear the memory cache, you can call the Cef.GetGlobalRequestContext().ClearCache(CefCacheType.MemoryCache) method.

It should be noted that since the memory cache is maintained by Chromium itself, we cannot directly control its size. If you need to control the cache size, you can indirectly control the size of the memory cache by setting the size of the disk cache.

1.2. Disk Cache

The disk cache is a file system-based cache that caches downloaded pages and resources. The disk cache is also limited in size, and when the cache reaches the maximum size, the oldest pages and resources are deleted.

CefSharp.WPF’s disk cache is implemented by setting the CachePath property in CefSettings. Specifically, we can set the path of the disk cache through the following code:

public partial class App : Application
 {
     protected override void OnStartup(StartupEventArgs e)
     {
         base.OnStartup(e);

         // CachePath needs to be an absolute path
         var settings = new CefSettings
         {
             CachePath = $"{AppDomain.CurrentDomain.BaseDirectory}DefaultCaches"
         };
         Cef.Initialize(settings);
     }
 }
 

The cache directory structure is as follows:

Among them, the CachePath attribute specifies the path of the disk cache (absolute path). If this property is not set, Chromium will store cache data in the default path (usually the AppData\Local\CefSharp directory in the user directory).

It should be noted that the size of the disk cache is controlled by Chromium itself. We can control the maximum space for cache data to be stored on the disk by setting the SetCacheLimit method of CacheController. This method accepts a long type parameter, indicating the maximum size of cached data (in bytes). For example, the following code sets the maximum size of the disk cache to 100MB:

var cacheController = Cef.GetGlobalRequestContext().CacheController;
 cacheController.SetCacheLimit(100 * 1024 * 1024); // 100MB
 

It should be noted that Chromium will automatically clear the least recently used cache data according to the LRU algorithm to make room for new data.if (dataIn == null)
{
return FilterStatus.NeedMoreData;
}

var length = dataIn.Length;
var data = new byte[BUFFER_LENGTH];
var count = dataIn.Read(data, 0, BUFFER_LENGTH);

dataInRead = count;
dataOutWritten = count;

dataOut?.Write(data, 0, count);

try
{
CacheFile(data, count);
}
catch
{
// ignored
}

return length == dataIn.Position ? FilterStatus.Done : FilterStatus.NeedMoreData;
}

public bool InitFilter()
{
try
{
var dirPath = Path.GetDirectoryName(LocalCacheFilePath);
if (!string.IsNullOrWhiteSpace(dirPath) && !Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
}
catch
{
// ignored
}

return true;
}

public void Dispose()
{
}

private void CacheFile(byte[] data, int count)
{
if (isFailCacheFile)
{
return;
}

try
{
if (!File.Exists(LocalCacheFilePath))
{
using var fs = File.Create(LocalCacheFilePath);
fs.Write(data, 0, count);
}
else
{
using var fs = File.Open(LocalCacheFilePath, FileMode.Append);
fs.Write(data,0,count);
}
}
catch
{
isFailCacheFile = true;
File.Delete(LocalCacheFilePath);
}
}
}

2.1.6. CacheFileHelper

Cache file helper class, used to manage the ajax interface cache whitelist, cache file path specifications, etc. of the page:

using CefSharp;
 using System;
 using System.Collections.Generic;
 using System.IO;

 namespace WpfWithCefSharpCacheDemo.Caches;

 internal static class CacheFileHelper
 {
     private const string DEV_TOOLS_SCHEME = "devtools";
     private const string DEFAULT_INDEX_FILE = "index.html";

     private static HashSet needInterceptedAjaxInterfaces = new();

     private static string CachePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "caches");

     public static void AddInterceptedAjaxInterfaces(string url)
     {
         if (needInterceptedAjaxInterfaces.Contains(url))
         {
             return;
         }

         needInterceptedAjaxInterfaces.Add(url);
     }

     private static bool IsNeedInterceptedAjaxInterface(string url, ResourceType resourceType)
     {
         var uri = new Uri(url);
         if (DEV_TOOLS_SCHEME == url)
         {
             return false;
         }

         if (ResourceType.Xhr == resourceType && !needInterceptedAjaxInterfaces.Contains(url))
         {
             return false;
         }

         return true;
     }

     public static string? CalculateResourceFileName(string url, ResourceType resourceType)
     {
         if (!IsNeedInterceptedAjaxInterface(url, resourceType))
         {
             return default;
         }

         var uri = new Uri(url);
         var urlPath = uri.LocalPath;

         if (urlPath.StartsWith("/"))
         {
             urlPath = urlPath.Substring(1);
         }

         var subFilePath = urlPath;
         if (ResourceType.MainFrame == resourceType || string.IsNullOrWhiteSpace(urlPath))
         {
             subFilePath = Path.Combine(urlPath, DEFAULT_INDEX_FILE);
         }

         var hostCachePath = Path.Combine(CachePath, uri.Host);
         var fullFilePath = Path.Combine(hostCachePath, subFilePath);
         return fullFilePath;
     }
 }
 

The subdirectory of the custom cache is created with the domain name (Host) of the resource as the directory name:

Open the cached dotnet9.com directory and check that the directory structure is basically the same as the program release directory. This is more suitable for people to see, right?

2.2. Possible problems

The first point is the problems currently encountered by webmasters. The next four points will be explained by Token AI.

2.2.1. Poor support for cached resource URLs with QueryString

It is recommended to use Route (routing method: https://dotnet9.com/albums/wpf) instead of QueryString (query parameter trial: https://dotnet9.com/albums?slug=wpf). When I have time, I will study the caching method of QueryString again.

If it is true that the resource contains QueryString, then let go of the cache for this resource and request it directly through the network.

2.2.2. Cache consistency problem

If a custom cache does not handle cache consistency correctly, it may cause the browser to display stale or inconsistent content. For example, if a web page is cached, but the web page has been updated on the server, it may cause the browser to display stale web content if the custom cache does not handle cache consistency correctly.

2.2.3. Cache space problem

If a custom cache does not manage cache space correctly, it may cause the browser to take up too much memory or disk space. For example, if a custom cache caches a large amount of data but does not clean up expired data in time or limit the size of the cache, it may cause the browser to occupy too much memory or disk space.

2.2.4. Cache performance problem

If custom caching does not handle cache performance correctly, it may cause browser performance degradation. For example, if a custom cache does not properly handle cached reads and writes, it may cause the browser to become less responsive.

2.2.5. Cache Security Issues

If a custom cache does not handle cache security correctly, the security of the browser may be compromised. For example, if a custom cache caches sensitive data but does not properly handle encryption and decryption of the cache, it may lead to sensitive data leakage.

Therefore, when customizing the cache, you need to pay attention to issues such as cache consistency, cache space, cache performance, and cache security to ensure the normal operation and security of the browser.

Reference:

  • CefSharp

  • About the application of mutual calling between C# and JS functions in CefSharp

  • WeChat technical exchange group: add WeChat (dotnet9com) remark “join the group”

  • QQ technical exchange group: 771992300.

Time is like running water, it can only flow away but never return.Definition cache does not handle cache consistency correctly, which may cause the browser to display stale web content.

2.2.3. Cache space problem

If a custom cache does not manage cache space correctly, it may cause the browser to take up too much memory or disk space. For example, if a custom cache caches a large amount of data but does not clean up expired data in time or limit the size of the cache, it may cause the browser to occupy too much memory or disk space.

2.2.4. Cache performance problem

If custom caching does not handle cache performance correctly, it may cause browser performance degradation. For example, if a custom cache does not properly handle cached reads and writes, it may cause the browser to become less responsive.

2.2.5. Cache Security Issues

If a custom cache does not handle cache security correctly, the security of the browser may be compromised. For example, if a custom cache caches sensitive data but does not properly handle encryption and decryption of the cache, it may lead to sensitive data leakage.

Therefore, when customizing the cache, you need to pay attention to issues such as cache consistency, cache space, cache performance, and cache security to ensure the normal operation and security of the browser.

Reference:

  • CefSharp

  • About the application of mutual calling between C# and JS functions in CefSharp

  • WeChat technical exchange group: add WeChat (dotnet9com) remark “join the group”

  • QQ technical exchange group: 771992300.

Time is like running water, it can only flow away but never return.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/cefsharp-custom-cache-implementation-2/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索