Base solution for your next web application
Open Closed

How to use Abp.RedisCache in ASP NET Zero ? #8406


User avatar
0
sumitshah created

We want to use the extention methods used in this package : KeyDeleteWithPrefix() and KeyCount()

We followed the ABP's Caching Documention and aded Abp.RedisCache in Web.Core's Project Module.

We added this in DependsOn Annotation and enable redis by using below

         //See app.config for Redis configuration and connection string
            Configuration.Caching.UseRedis(options =>
            {
                options.ConnectionString = _appConfiguration["Abp:RedisCache:ConnectionString"];
                options.DatabaseId = _appConfiguration.GetValue<int>("Abp:RedisCache:DatabaseId");
            });   

Then I created a helper class for Redis Caching and injected

    public class RedisCacheHelper : IRedisCacheHelper
    {
        private readonly IConfigurationRoot _appConfiguration;
        private readonly AbpRedisCacheManager _cacheManger;
        public RedisCacheHelper(IHostingEnvironment env, AbpRedisCacheManager cacheManger)
        {
            _appConfiguration = env.GetAppConfiguration();
            _cacheManger = cacheManger;
        }
    }

Earlier we were using IDistributedCache in place of AbpRedisCacheManager. After doing these steps we are not able to find any methods to add or get value with particular key in Redis cache like we had in IDistributedCache. We also not able to get redis database (IDatabase) which contains the methods KeyDeleteWithPrefix() and KeyCount()

How can we implement this ?


6 Answer(s)
  • User Avatar
    1
    maliming created
    Support Team

    After doing these steps we are not able to find any methods to add or get value with particular key in Redis cache like we had in IDistributedCache.

    Can you explain it in detail?

    We also not able to get redis database (IDatabase) which contains the methods KeyDeleteWithPrefix() and KeyCount()

    IDatabase database = redisCacheDatabaseProvider.GetDatabase();
    
    database.KeyDeleteWithPrefix()
    database.KeyCount()
    

    https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp.RedisCache/Runtime/Caching/Redis/AbpRedisCache.cs#L29

  • User Avatar
    0
    sumitshah created

    My requirement is to perform the following operations :

    1. Get value from cache
    2. Set value in cache
    3. Delete by Prefix
    4. Get count by Prefix

    I am implementing a helper class which fetches the AbsoluteExpiration and SlidingExpiration from the appsetting.json files.

    I see there are Get() and Set() methods inside AbpRedisCache class but I also need access to IDatabase to fire database.KeyDeleteWithPrefix()

    Is it AbpRedisCache or IAbpRedisCacheDatabaseProvider

  • User Avatar
    0
    ismcagdas created
    Support Team

    @sumitshah

    You can inject IAbpRedisCacheDatabaseProvider and get the database using redisCacheDatabaseProvider.GetDatabase(). Then, you can use KeyDeleteWithPrefix method.

  • User Avatar
    0
    sumitshah created

    As RedisDatabaseExtensions class was internal we had to copy its implementations to our helper class. Doing so we were able to get "count by key prefix".

    However, after migrating from .Net IDistributedCache to Abp.RedisCache package we observed that values are now being stored as string type in Redis instead of hash type.

    As hash type is much safer we want to contine using it.

    Is there any way to store the values in Hash format.

  • User Avatar
    0
    maliming created
    Support Team

    hi @sumitshah

    The current design is to store the value as a string. You can use the redis API to Implement your own needs.

  • User Avatar
    0
    ismcagdas created
    Support Team

    This issue is closed because of no recent activity. Please create a new issue if you are still having this problem.