Link to home
Start Free TrialLog in
Avatar of srikotesh
srikotesh

asked on

problem with Redis Cache in my java code

Hi Experts,

I am using Redis Cache
First time only my method will hit DB and get the values
Next time if i want same values i am going to get it from redis cache manager.

problem is
some time redis server going down.
that time my code breaking down.

i want to do check like

if the server is down skip getting the values from the cache manager,need to get it from DB.
even when i moved to other environment like dev environment to production
if the redis server not installed that time also my code should not break.

can some one suggest me how to do?

 @Autowired
    private CacheManager<UserGroupToGroupType> cacheManager;
    
    @Override
    public List<UserGroupToGroupTypeDTO> getGroupForCustomerIdAndGroupType(String customerId, Integer groupTypeId) {
        UserGroupToGroupType groupToGroupType=cacheManager.findById(customerId);
        if(groupToGroupType!=null){
            return groupToGroupType.getUserGroupToGroupTypeDTOs();
        }
        List<Object[]> namedFiltersList = userGrpToGrpDao.getGroupForCustomerIdAndGroupType(customerId, groupTypeId,
                UserGroupToGroupTypeDAOImpl.Readset.ID_GID_GNAME.getName());
        return UserGroupToGroupTypeServiceHelper.convertDotoDTOforID_GID_GNAME(namedFiltersList,cacheManager,customerId);
    }
}

Open in new window

Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

I dont know anything about reds but doesn't findById on CacheManager throw an exception?
Avatar of srikotesh
srikotesh

ASKER

yes connection failure exception will come.
my code will skip from that line.
Then jus t simply catch the exception
I can place customized exception, if I place catch exception whenever redis cache is down it will throw this exception.

it will not execute the rest of the lines.//db call



shall I keep db calls statement in finally block.
is that good way to code?
if I keep db call code in finally block my main objective will be spoiled.

redis cache is used to get the values from cache.
so we can reduce the db hit.
ASKER CERTIFIED SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi gurpsbassi,

From the 5th line we are calling findFromCache method if it throws exception.
will it executes the 7,8,9,10 steps?
if it executes the rest of the statements then it will be good.
The exception will be caught. If you look, there is a catch block.
findFromCache will return null.