I have a cache with objects which were read from the database (regular beans).
When I return them I want to make them read only so users of the cache can't damage the cache objects.
I'm looking for an efficient way since objects can be requested thousands of times per second.
One solution is to wrap each object with a proxy object which disables all the set accessors (can be done with Java Assist) but I'm looking for a more lightweight solution.
Another solution is to clone the object before returning it but it is even wrost than the proxy solution performance wise.
Asked
Active
Viewed 55 times
0

Tim
- 153
- 1
- 7

Avner Levy
- 6,601
- 9
- 53
- 92
-
1can't you make them immutable by design? – Adi Aug 27 '14 at 13:51
-
1I think the simplest way would be to have a constructor which takes all read-only properties, and to remove all the setters for read-only attributes. Obviously the attributes should be `final` and `private` – BackSlash Aug 27 '14 at 13:54
-
How is the wrapper not lightweight? – Jean Logeart Aug 27 '14 at 13:55