Create Client Cache with NuGet Packages Installation
If you're using NCache NuGet packages and want to create a client cache using your application without having NCache installed, then NCache allows you to do so in the following steps:
Prerequisites for Creating Client Caches
The required NuGet package must be already installed in your application. On successful installation of the NuGet package, it copies these two configuration files in your application's local directory:
- config.ncconf: It is used for local InProc cache and InProc client caches. For the client cache, the config.ncconf file must also contain the name of the clustered cache that this client cache is a part of.
- client.ncconf: It contains information about the cache servers of each cache that the application needs to access.
Step 1: Edit NCache Configuration File
- Open the configurations copied from the config.ncconf file. The configuration file comes with a default local cache by the name of mycache.
- If you already have the configuration of the client cache, add the configuration under the
<configuration>
tag. - If you do not have the configuration of the client cache, copy the following configuration and paste it under the
<configuration>
tag.
Note
It is recommended that you use JSON serialization if you are using ASP.NET 5.0 and above. For more details, click here.
<cache-config cache-name="demoClientCache" environment="" config-id="48bf4956-6d5b-4704-bb4a-254ca66a567b" config-version="0">
<cache-settings inproc="True" last-modified="" auto-start="False" data-format="Object" serialization="Binary">
<logging enable-logs="True" trace-errors="True" trace-notices="False" trace-warnings="False" trace-debug="False" log-path=""/>
<performance-counters enable-counters="True" snmp-port="0"/>
<compression enable-compression="False" threshold="100kb"/>
<client-death-detection/>
<client-activity-notification enabled="False" retention-period="5sec"/>
<cache-notifications item-remove="False" item-add="False" item-update="False" expiration-time="15sec"/>
<cleanup interval="15sec"/>
<storage type="heap" cache-size="1024mb"/>
<eviction-policy enabled-eviction="True" default-priority="normal" policy="lru" eviction-ratio="5%"/>
<expiration-policy enabled="False">
<absolute-expiration longer-enabled="False" longer-value="0" default-enabled="False" default-value="0"/>
<sliding-expiration longer-enabled="False" longer-value="0" default-enabled="False" default-value="0"/>
</expiration-policy>
<sql-dependency use-default="True"/>
<synchronization strategy="polling" polling-interval="10sec"/>
<cache-topology topology="client-cache"/>
<client-cache-settings server-cache="demoCache" persist-queue="False" enable-disconnected="False" offline-queue-size="0"/>
<tasks-config max-tasks="10" chunk-size="100" communicate-stats="False" queue-size="10" max-avoidable-exceptions="10"/>
<split-brain-recovery enable="False" detection-interval="60"/>
</cache-settings>
</cache-config>
Step 2: Update Cache Names in Cache Configuration
- Update the name of the client cache according to your own client cache name in the following tag in the config.ncconf file:
<cache-config cache-name="demoClientCache" ... />
- Change the name of the clustered cache that the client cache is a part of and your application will connect to in the config.ncconf file:
<cache-settings>
<client-cache-settings server-cache="demoCache" ... />
</cache-settings>
Step 3: Create Cache Connection
After modifying the cache configuration, the client application will need to create the cache connection and that can be done by the following two methods:
Method 1: Modify client.ncconf
From your application's local directory, open client.ncconf. Add the cache configurations as shown below:
- Clustered Cache:
This is the clustered cache that the client cache is a part of and with which your application connects. Make sure you provide your own cache's name as provided in step 2 and the IP for the cluster in the<server name = ... >
tag.
<cache id="demoCache" client-cache-id="demoClientCache" client-cache-syncmode="optimistic" default-readthru-provider="" default-writethru-provider="" load-balance="True" enable-client-logs="False" log-level="error">
<server name="20.200.20.39"/>
</cache>
Method 2: Using Client Connection Options
You can connect to the cache and client cache using the GetCache method. For more details, refer to the Connect to Clustered and Client Cache section in the Programmers' Guide.
// Specify the cache names
string clusteredCache = "demoCache";
string clientCache = "demoClientCache";
CacheConnectionOptions cacheConnectionOptions = new CacheConnectionOptions();
cacheConnectionOptions.LoadBalance = true;
cacheConnectionOptions.ConnectionRetries = 5;
CacheConnectionOptions clientCacheConnectionOptions = new CacheConnectionOptions();
clientCacheConnectionOptions.LoadBalance = true;
clientCacheConnectionOptions.ConnectionRetries = 5;
clientCacheConnectionOptions.Mode = IsolationLevel.OutProc;
// Connect to the caches in a single call
ICache cache = CacheManager.GetCache(clusteredCache, cacheConnectionOptions, clientCache, clientCacheConnectionOptions);
You can now perform operations on your client cache easily.
See Also
Enable Client Cache on Client Nodes
Disable Client Cache on Client Nodes
Remove Client Cache
Management Operations