Issue
So I'm trying to implement a caching service using redis in spring.this is The code for the caching service.
@Service
public class PermissionCachingService {
@Autowired
PermissionRepository permissionRepository;
@CachePut(value = "Permission", key = "#permission.id")
public Permission save(Permission permission) {
return permissionRepository.save(permission);
}
@CacheEvict(value = "Permission", key = "#permission.id")
void delete(Permission permission) {
permissionRepository.delete(permission);
}
}
This is the object I'm trying to cache.
@Entity
public class Permission {
@Id
@NotNull
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@NotNull
Boolean read = false;
@NotNull
Boolean write = false;
@NotNull
Boolean update = false;
@NotNull
@ManyToOne
Resource resource;
Long groupId;
//construct
//setters and getters
}
the save works fine for both saving/updating. however, the delete is doing nothing to the cache whatsoever. I tried evicting all entries, even hardcoding a key to delete, but it's doing nothing.
I tried using CacheManager, and it cleared the "Permission" cache successfully with the getCache("Permission").clear() method. However, evicting using a key didn't work, even though I used the same key as the one in the save. what's wrong with the code?
Solution
The delete method just wasn't public. After making it public, everything worked as intended.
@CacheEvict(value = "Permission", key = "#permission.id")
public void delete(Permission permission) {
permissionRepository.delete(permission);
}
Answered By - Karim Halayqa
Answer Checked By - Marie Seifert (JavaFixing Admin)