@@ -43,7 +43,12 @@ public class Trackable(StorageItem item, TrackState state)
4343        } 
4444
4545        private  readonly  Dictionary < StorageKey ,  Trackable >  _dictionary  =  [ ] ; 
46-         private  readonly  HashSet < StorageKey >  _changeSet  =  [ ] ; 
46+         private  readonly  HashSet < StorageKey > ?  _changeSet ; 
47+ 
48+         /// <summary> 
49+         /// True if DataCache is readOnly 
50+         /// </summary> 
51+         public  bool  IsReadOnly  =>  _changeSet  ==  null ; 
4752
4853        /// <summary> 
4954        /// Reads a specified entry from the cache. If the entry is not in the cache, it will be automatically loaded from the underlying storage. 
@@ -72,6 +77,16 @@ public StorageItem this[StorageKey key]
7277            } 
7378        } 
7479
80+         /// <summary> 
81+         /// Data cache constructor 
82+         /// </summary> 
83+         /// <param name="readOnly">True if you don't want to allow writes</param> 
84+         protected  DataCache ( bool  readOnly ) 
85+         { 
86+             if  ( ! readOnly ) 
87+                 _changeSet  =  [ ] ; 
88+         } 
89+ 
7590        /// <summary> 
7691        /// Adds a new entry to the cache. 
7792        /// </summary> 
@@ -97,7 +112,7 @@ public void Add(StorageKey key, StorageItem value)
97112                { 
98113                    _dictionary [ key ]  =  new  Trackable ( value ,  TrackState . Added ) ; 
99114                } 
100-                 _changeSet . Add ( key ) ; 
115+                 _changeSet ? . Add ( key ) ; 
101116            } 
102117        } 
103118
@@ -113,6 +128,11 @@ public void Add(StorageKey key, StorageItem value)
113128        /// </summary> 
114129        public  virtual  void  Commit ( ) 
115130        { 
131+             if  ( _changeSet  is  null ) 
132+             { 
133+                 throw  new  InvalidOperationException ( "DataCache is read only" ) ; 
134+             } 
135+ 
116136            lock  ( _dictionary ) 
117137            { 
118138                foreach  ( var  key  in  _changeSet ) 
@@ -138,6 +158,24 @@ public virtual void Commit()
138158            } 
139159        } 
140160
161+         /// <summary> 
162+         /// Gets the change set in the cache. 
163+         /// </summary> 
164+         /// <returns>The change set.</returns> 
165+         public  IEnumerable < KeyValuePair < StorageKey ,  Trackable > >  GetChangeSet ( ) 
166+         { 
167+             if  ( _changeSet  is  null ) 
168+             { 
169+                 throw  new  InvalidOperationException ( "DataCache is read only" ) ; 
170+             } 
171+ 
172+             lock  ( _dictionary ) 
173+             { 
174+                 foreach  ( var  key  in  _changeSet ) 
175+                     yield  return  new ( key ,  _dictionary [ key ] ) ; 
176+             } 
177+         } 
178+ 
141179        /// <summary> 
142180        /// Creates a snapshot, which uses this instance as the underlying storage. 
143181        /// </summary> 
@@ -170,20 +208,20 @@ public void Delete(StorageKey key)
170208                    if  ( trackable . State  ==  TrackState . Added ) 
171209                    { 
172210                        trackable . State  =  TrackState . NotFound ; 
173-                         _changeSet . Remove ( key ) ; 
211+                         _changeSet ? . Remove ( key ) ; 
174212                    } 
175213                    else  if  ( trackable . State  !=  TrackState . NotFound ) 
176214                    { 
177215                        trackable . State  =  TrackState . Deleted ; 
178-                         _changeSet . Add ( key ) ; 
216+                         _changeSet ? . Add ( key ) ; 
179217                    } 
180218                } 
181219                else 
182220                { 
183221                    var  item  =  TryGetInternal ( key ) ; 
184222                    if  ( item  ==  null )  return ; 
185223                    _dictionary . Add ( key ,  new  Trackable ( item ,  TrackState . Deleted ) ) ; 
186-                     _changeSet . Add ( key ) ; 
224+                     _changeSet ? . Add ( key ) ; 
187225                } 
188226            } 
189227        } 
@@ -262,19 +300,6 @@ public void Delete(StorageKey key)
262300                    yield  break ; 
263301        } 
264302
265-         /// <summary> 
266-         /// Gets the change set in the cache. 
267-         /// </summary> 
268-         /// <returns>The change set.</returns> 
269-         public  IEnumerable < KeyValuePair < StorageKey ,  Trackable > >  GetChangeSet ( ) 
270-         { 
271-             lock  ( _dictionary ) 
272-             { 
273-                 foreach  ( var  key  in  _changeSet ) 
274-                     yield  return  new ( key ,  _dictionary [ key ] ) ; 
275-             } 
276-         } 
277- 
278303        /// <summary> 
279304        /// Determines whether the cache contains the specified entry. 
280305        /// </summary> 
@@ -337,13 +362,13 @@ public bool Contains(StorageKey key)
337362                        else 
338363                        { 
339364                            trackable . State  =  TrackState . Added ; 
340-                             _changeSet . Add ( key ) ; 
365+                             _changeSet ? . Add ( key ) ; 
341366                        } 
342367                    } 
343368                    else  if  ( trackable . State  ==  TrackState . None ) 
344369                    { 
345370                        trackable . State  =  TrackState . Changed ; 
346-                         _changeSet . Add ( key ) ; 
371+                         _changeSet ? . Add ( key ) ; 
347372                    } 
348373                } 
349374                else 
@@ -359,7 +384,7 @@ public bool Contains(StorageKey key)
359384                        trackable  =  new  Trackable ( item ,  TrackState . Changed ) ; 
360385                    } 
361386                    _dictionary . Add ( key ,  trackable ) ; 
362-                     _changeSet . Add ( key ) ; 
387+                     _changeSet ? . Add ( key ) ; 
363388                } 
364389                return  trackable . Item ; 
365390            } 
@@ -392,7 +417,7 @@ public StorageItem GetOrAdd(StorageKey key, Func<StorageItem> factory)
392417                        else 
393418                        { 
394419                            trackable . State  =  TrackState . Added ; 
395-                             _changeSet . Add ( key ) ; 
420+                             _changeSet ? . Add ( key ) ; 
396421                        } 
397422                    } 
398423                } 
@@ -402,7 +427,7 @@ public StorageItem GetOrAdd(StorageKey key, Func<StorageItem> factory)
402427                    if  ( item  ==  null ) 
403428                    { 
404429                        trackable  =  new  Trackable ( factory ( ) ,  TrackState . Added ) ; 
405-                         _changeSet . Add ( key ) ; 
430+                         _changeSet ? . Add ( key ) ; 
406431                    } 
407432                    else 
408433                    { 
0 commit comments