ImageResizer  3.4.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Public Member Functions | Protected Member Functions | Protected Attributes | Properties | Events | List of all members
ImageResizer.Plugins.DiskCache.CachedFolder Class Reference

Represents a cached view of a folder of cached items More...

Inheritance diagram for ImageResizer.Plugins.DiskCache.CachedFolder:
Inheritance graph
[legend]

Public Member Functions

virtual void clear ()
 
virtual CachedFileInfo getCachedFileInfo (string relativePath)
 Returns null if (a) the file doesn't exist, or (b) the file isn't populated. Calling code should always fall back to filesystem calls on a null result. More...
 
virtual void setCachedFileInfo (string relativePath, CachedFileInfo info)
 Sets the CachedFileInfo object for the specified path, creating any needed folders along the way. If 'null', the item will be removed, and no missing folder will be created. More...
 
bool bumpDateIfExists (string relativePath)
 Tries to set the AccessedUtc of the specified file to the current date (just in memory, not on the filesystem). More...
 
virtual CachedFileInfo getFileInfo (string relativePath, string physicalPath)
 Gets a CachedFileInfo object for the file even if it isn't in the cache (falls back to the filesystem) More...
 
virtual CachedFileInfo getFileInfoCertainExists (string relativePath, string physicalPath)
 Verifies the file exists before returning the cached data. Discrepancies in file existence result in OnFileDisappeard being fired. More...
 
bool GetIsValid (string relativePath)
 Returns the value of IsValid on the specified folder if present, or 'false' if not present. More...
 
IList< string > getSubfolders (string relativePath)
 returns a list More...
 
Dictionary< string,
CachedFileInfo
getSubfilesCopy (string relativePath)
 returns a dictionary of files. More...
 
ICollection< KeyValuePair
< string, CachedFileInfo > > 
getSortedSubfiles (string relativePath)
 returns a dictionary of files. More...
 
int getFileCount (string relativePath)
 
void populate (string relativePath, string physicalPath)
 Refreshes file and folder listing for this folder (non-recursive). Sets IsValid=true afterwards. More...
 
bool existsCertain (string relativePath, string physicalPath)
 
bool exists (string relativePath, string physicalPath)
 
bool modifiedDateMatches (DateTime utc, string relativePath, string physicalPath)
 
bool modifiedDateMatchesCertainExists (DateTime utc, string relativePath, string physicalPath)
 

Protected Member Functions

CachedFolder getFolder (string relativePath)
 Not thread safe. More...
 
CachedFolder getOrCreateFolder (string relativePath, bool createIfMissing)
 
void populateSubfolders (string relativePath, string physicalPath)
 Updates the 'folders' dictionary to match the folders that exist on disk. ONLY UPDATES THE LOCAL FOLDER More...
 
void populateFiles (string relativePath, string physicalPath)
 Updates the 'files' dictionary to match the files that exist on disk. Uses the accessedUtc values from the previous dictionary if they are newer. More...
 
bool roughCompare (DateTime d1, DateTime d2)
 Returns true if both dates are equal to the nearest 200th of a second. More...
 
string checkRelativePath (string relativePath)
 

Protected Attributes

readonly object _sync = new object()
 
Dictionary< string, CachedFolderfolders = new Dictionary<string, CachedFolder>(StringComparer.OrdinalIgnoreCase)
 
Dictionary< string,
CachedFileInfo
files = new Dictionary<string, CachedFileInfo>(StringComparer.OrdinalIgnoreCase)
 

Properties

bool IsValid [get, set]
 Defaults to false. Set to true immediately after being refreshed from the filesystem. Set to false if a file disappears from the filesystem cache without the cache index being notified first. Used by the cleanup system - not of importance to the cache write system. More...
 
StringComparer KeyComparer [get]
 

Events

FileDisappearedHandler FileDisappeared
 Fired when a file disappears from the cache folder without the cache index knowing about it. More...
 

Detailed Description

Represents a cached view of a folder of cached items

Definition at line 14 of file CachedFolder.cs.

Member Function Documentation

bool ImageResizer.Plugins.DiskCache.CachedFolder.bumpDateIfExists ( string  relativePath)
inline

Tries to set the AccessedUtc of the specified file to the current date (just in memory, not on the filesystem).

Parameters
relativePath
Returns

Definition at line 114 of file CachedFolder.cs.

114  {
115  relativePath = checkRelativePath(relativePath);
116  lock (_sync) {
117  int slash = relativePath.IndexOf('/');
118  if (slash < 0) {
119  //Update the accessed date.
120  CachedFileInfo old;
121  if (files.TryGetValue(relativePath,out old)) files[relativePath] = new CachedFileInfo(old, DateTime.UtcNow);
122  return true; //We updated it!
123  } else {
124  //Try to access subfolder
125  string folder = relativePath.Substring(0, slash);
126  CachedFolder f = null;
127  if (!folders.TryGetValue(folder, out f)) return false;//If the folder doesn't exist, quit
128  if (f == null) return false; //If the folder is null, quit!
129 
130  //Recurse if possible
131  return f.bumpDateIfExists(relativePath.Substring(slash + 1));
132  }
133  }
134  }
string checkRelativePath(string relativePath)
string ImageResizer.Plugins.DiskCache.CachedFolder.checkRelativePath ( string  relativePath)
inlineprotected

Parameters
relativePath
Returns

Definition at line 399 of file CachedFolder.cs.

399  {
400  if (relativePath == null) return relativePath;
401  if (relativePath.StartsWith("/") || relativePath.EndsWith("/")) {
402  Debug.WriteLine("Invalid relativePath value - should never have leading slash!");
403  }
404  return relativePath;
405  }
virtual CachedFileInfo ImageResizer.Plugins.DiskCache.CachedFolder.getCachedFileInfo ( string  relativePath)
inlinevirtual

Returns null if (a) the file doesn't exist, or (b) the file isn't populated. Calling code should always fall back to filesystem calls on a null result.

Parameters
relativePath
Returns

Definition at line 59 of file CachedFolder.cs.

59  {
60  relativePath = checkRelativePath(relativePath);
61  lock (_sync) {
62  int slash = relativePath.IndexOf('/');
63  if (slash < 0) {
64  CachedFileInfo f;
65  if (files.TryGetValue(relativePath, out f)) return f; //cache hit
66  } else {
67  //Try to access subfolder
68  string folder = relativePath.Substring(0, slash);
69  CachedFolder f;
70  if (!folders.TryGetValue(folder, out f)) f = null;
71  //Recurse if possible
72  if (f != null) return f.getCachedFileInfo(relativePath.Substring(slash + 1));
73  }
74  return null; //cache miss or file not found
75  }
76  }
string checkRelativePath(string relativePath)
virtual CachedFileInfo ImageResizer.Plugins.DiskCache.CachedFolder.getFileInfo ( string  relativePath,
string  physicalPath 
)
inlinevirtual

Gets a CachedFileInfo object for the file even if it isn't in the cache (falls back to the filesystem)

Parameters
relativePath
physicalPath
Returns

Definition at line 142 of file CachedFolder.cs.

142  {
143  relativePath = checkRelativePath(relativePath);
144  lock (_sync) {
145  CachedFileInfo f = getCachedFileInfo(relativePath);
146  //On cache miss or no file
147  if (f == null && System.IO.File.Exists(physicalPath)) {
148  //on cache miss
149  f = new CachedFileInfo(new System.IO.FileInfo(physicalPath));
150  //Populate cache
151  setCachedFileInfo(relativePath, f);
152  }
153  return f;//Null only if the file doesn't exist.
154  }
155  }
virtual void setCachedFileInfo(string relativePath, CachedFileInfo info)
Sets the CachedFileInfo object for the specified path, creating any needed folders along the way...
Definition: CachedFolder.cs:84
string checkRelativePath(string relativePath)
virtual CachedFileInfo getCachedFileInfo(string relativePath)
Returns null if (a) the file doesn&#39;t exist, or (b) the file isn&#39;t populated. Calling code should alwa...
Definition: CachedFolder.cs:59
virtual CachedFileInfo ImageResizer.Plugins.DiskCache.CachedFolder.getFileInfoCertainExists ( string  relativePath,
string  physicalPath 
)
inlinevirtual

Verifies the file exists before returning the cached data. Discrepancies in file existence result in OnFileDisappeard being fired.

Parameters
relativePath
physicalPath
Returns

Definition at line 163 of file CachedFolder.cs.

163  {
164  relativePath = checkRelativePath(relativePath);
165  bool fireEvent = false;
166  CachedFileInfo f = null;
167  lock (_sync) {
168  bool exists = System.IO.File.Exists(physicalPath);
169 
170  f = getCachedFileInfo(relativePath);
171  //cache miss
172  if (f == null && exists) {
173  //on cache miss
174  f = new CachedFileInfo(new System.IO.FileInfo(physicalPath));
175  //Populate cache
176  setCachedFileInfo(relativePath, f);
177  }
178  //cache wrong, discrepancy. File deleted by external actor
179  if (f != null && !exists) {
180  f = null;
181  clear(); //Clear the cache completely.
182  fireEvent = true;
183  }
184  }
185  //Fire the event outside of the lock.
186  if (fireEvent && FileDisappeared != null) FileDisappeared(relativePath, physicalPath);
187 
188  return f;//Null only if the file doesn't exist.
189  }
virtual void setCachedFileInfo(string relativePath, CachedFileInfo info)
Sets the CachedFileInfo object for the specified path, creating any needed folders along the way...
Definition: CachedFolder.cs:84
FileDisappearedHandler FileDisappeared
Fired when a file disappears from the cache folder without the cache index knowing about it...
Definition: CachedFolder.cs:36
string checkRelativePath(string relativePath)
virtual CachedFileInfo getCachedFileInfo(string relativePath)
Returns null if (a) the file doesn&#39;t exist, or (b) the file isn&#39;t populated. Calling code should alwa...
Definition: CachedFolder.cs:59
CachedFolder ImageResizer.Plugins.DiskCache.CachedFolder.getFolder ( string  relativePath)
inlineprotected

Not thread safe.

Parameters
relativePath
Returns

Definition at line 208 of file CachedFolder.cs.

208  {
209  return getOrCreateFolder(relativePath, false);
210  }
bool ImageResizer.Plugins.DiskCache.CachedFolder.GetIsValid ( string  relativePath)
inline

Returns the value of IsValid on the specified folder if present, or 'false' if not present.

Parameters
relativePath
Returns

Definition at line 196 of file CachedFolder.cs.

196  {
197  lock (_sync) {
198  CachedFolder f = getFolder(relativePath);
199  if (f != null) return f.IsValid;
200  return false;
201  }
202  }
CachedFolder getFolder(string relativePath)
Not thread safe.
ICollection<KeyValuePair<string, CachedFileInfo> > ImageResizer.Plugins.DiskCache.CachedFolder.getSortedSubfiles ( string  relativePath)
inline

returns a dictionary of files.

Parameters
relativePath
Returns

Definition at line 262 of file CachedFolder.cs.

262  {
263  lock (_sync) {
264  CachedFolder f = getFolder(relativePath);
265  if (f == null || f.files.Count < 1) return null;
266  //Copy pairs to an array.
267  KeyValuePair<string, CachedFileInfo>[] items = new KeyValuePair<string, CachedFileInfo>[f.files.Count];
268  int i = 0;
269  foreach (KeyValuePair<string, CachedFileInfo> pair in f.files) {
270  items[i] = pair;
271  i++;
272  }
273  //Sort the pairs on accessed date
274  Array.Sort<KeyValuePair<string, CachedFileInfo>>(items, delegate(KeyValuePair<string, CachedFileInfo> a, KeyValuePair<string, CachedFileInfo> b) {
275  return DateTime.Compare(a.Value.AccessedUtc, b.Value.AccessedUtc);
276  });
277 
278 
279  return items;
280  }
281  }
CachedFolder getFolder(string relativePath)
Not thread safe.
Dictionary<string, CachedFileInfo> ImageResizer.Plugins.DiskCache.CachedFolder.getSubfilesCopy ( string  relativePath)
inline

returns a dictionary of files.

Parameters
relativePath
Returns

Definition at line 251 of file CachedFolder.cs.

251  {
252  lock (_sync) {
253  CachedFolder f = getFolder(relativePath);
254  return new Dictionary<string, CachedFileInfo>(f.files, f.files.Comparer);
255  }
256  }
CachedFolder getFolder(string relativePath)
Not thread safe.
IList<string> ImageResizer.Plugins.DiskCache.CachedFolder.getSubfolders ( string  relativePath)
inline

returns a list

Parameters
relativePath
Returns

Definition at line 239 of file CachedFolder.cs.

239  {
240  lock (_sync) {
241  CachedFolder f = getFolder(relativePath);
242  return new List<string>(f.folders.Keys);
243  }
244  }
CachedFolder getFolder(string relativePath)
Not thread safe.
void ImageResizer.Plugins.DiskCache.CachedFolder.populate ( string  relativePath,
string  physicalPath 
)
inline

Refreshes file and folder listing for this folder (non-recursive). Sets IsValid=true afterwards.

Parameters
relativePath
physicalPath

Definition at line 296 of file CachedFolder.cs.

296  {
297  //NDJ-added May 29,2011
298  //Nothing was setting IsValue=true before.
299  populateSubfolders(relativePath, physicalPath);
300  populateFiles(relativePath, physicalPath);
301  getOrCreateFolder(relativePath, true).IsValid = true;
302  }
void populateSubfolders(string relativePath, string physicalPath)
Updates the &#39;folders&#39; dictionary to match the folders that exist on disk. ONLY UPDATES THE LOCAL FOLD...
void populateFiles(string relativePath, string physicalPath)
Updates the &#39;files&#39; dictionary to match the files that exist on disk. Uses the accessedUtc values fro...
void ImageResizer.Plugins.DiskCache.CachedFolder.populateFiles ( string  relativePath,
string  physicalPath 
)
inlineprotected

Updates the 'files' dictionary to match the files that exist on disk. Uses the accessedUtc values from the previous dictionary if they are newer.

Parameters
relativePath
physicalPath

Definition at line 335 of file CachedFolder.cs.

335  {
336  relativePath = checkRelativePath(relativePath);
337  string[] physicalFiles = null;
338  try {
339  physicalFiles = System.IO.Directory.GetFiles(physicalPath);
340  } catch (DirectoryNotFoundException) {
341  physicalFiles = new string[] { }; //Pretend it's empty. We don't care, the next recursive will get rid of it.
342  }
343  Dictionary<string, CachedFileInfo> newFiles = new Dictionary<string, CachedFileInfo>(physicalFiles.Length, KeyComparer);
344 
345  CachedFolder f = getOrCreateFolder(relativePath, true);
346  foreach (string s in physicalFiles) {
347  string local = s.Substring(s.LastIndexOf(System.IO.Path.DirectorySeparatorChar) + 1);
348 
349  //Todo, add a callback that handles exclusion of files
350  if (local.EndsWith(".config", StringComparison.OrdinalIgnoreCase)) continue;
351  if (local.StartsWith(".")) continue; //Skip files that start with a period
352 
353  //What did we have on file?
354  CachedFileInfo old = null;
355  lock (_sync) {
356  if (!f.files.TryGetValue(relativePath, out old)) old = null;
357  }
358  newFiles[local] = new CachedFileInfo(new FileInfo(s), old);
359  }
360  lock (_sync) {
361  f.files = newFiles;
362  }
363  }
string checkRelativePath(string relativePath)
void ImageResizer.Plugins.DiskCache.CachedFolder.populateSubfolders ( string  relativePath,
string  physicalPath 
)
inlineprotected

Updates the 'folders' dictionary to match the folders that exist on disk. ONLY UPDATES THE LOCAL FOLDER

Parameters
relativePath
physicalPath

Definition at line 308 of file CachedFolder.cs.

308  {
309  relativePath = checkRelativePath(relativePath);
310  string[] dirs = null;
311  try {
312  dirs = System.IO.Directory.GetDirectories(physicalPath);
313  } catch (DirectoryNotFoundException) {
314  dirs = new string[]{}; //Pretend it's empty. We don't care, the next recursive will get rid of it.
315  }
316  lock (_sync) {
317  CachedFolder f = getOrCreateFolder(relativePath, true);
318  Dictionary<string, CachedFolder> newFolders = new Dictionary<string, CachedFolder>(dirs.Length, KeyComparer);
319  foreach (string s in dirs) {
320  string local = s.Substring(s.LastIndexOf(System.IO.Path.DirectorySeparatorChar) + 1);
321  if (local.StartsWith(".")) continue; //Skip folders that start with a period.
322  if (f.folders.ContainsKey(local))
323  newFolders[local] = f.folders[local]; //What if the value is null? does containskey work?
324  else
325  newFolders[local] = new CachedFolder();
326  }
327  f.folders = newFolders; //Question - why didn't the folders ge tlisted?
328  }
329  }
string checkRelativePath(string relativePath)
bool ImageResizer.Plugins.DiskCache.CachedFolder.roughCompare ( DateTime  d1,
DateTime  d2 
)
inlineprotected

Returns true if both dates are equal to the nearest 200th of a second.

Parameters
d1
d2
Returns

Definition at line 390 of file CachedFolder.cs.

390  {
391  return Math.Abs(d1.Ticks - d2.Ticks) < TimeSpan.TicksPerMillisecond * 5;
392  }
virtual void ImageResizer.Plugins.DiskCache.CachedFolder.setCachedFileInfo ( string  relativePath,
CachedFileInfo  info 
)
inlinevirtual

Sets the CachedFileInfo object for the specified path, creating any needed folders along the way. If 'null', the item will be removed, and no missing folder will be created.

Parameters
relativePath
info

Definition at line 84 of file CachedFolder.cs.

84  {
85  relativePath = checkRelativePath(relativePath);
86  lock (_sync) {
87  int slash = relativePath.IndexOf('/');
88  if (slash < 0) {
89  //Set or remove the file
90  if (info == null)
91  files.Remove(relativePath);
92  else
93  files[relativePath] = info;
94  } else {
95  //Try to access subfolder
96  string folder = relativePath.Substring(0, slash);
97  CachedFolder f;
98  if (!folders.TryGetValue(folder, out f)) f = null;
99 
100 
101  if (info == null && f == null) return; //If the folder doesn't exist, the file definitely doesn't. Already accomplished.
102  //Create it if it doesn't exist
103  if (f == null) f = folders[folder] = new CachedFolder();
104  //Recurse if possible
105  f.setCachedFileInfo(relativePath.Substring(slash + 1), info);
106  }
107  }
108  }
string checkRelativePath(string relativePath)

Property Documentation

bool ImageResizer.Plugins.DiskCache.CachedFolder.IsValid
getset

Defaults to false. Set to true immediately after being refreshed from the filesystem. Set to false if a file disappears from the filesystem cache without the cache index being notified first. Used by the cleanup system - not of importance to the cache write system.

Definition at line 25 of file CachedFolder.cs.

Event Documentation

FileDisappearedHandler ImageResizer.Plugins.DiskCache.CachedFolder.FileDisappeared

Fired when a file disappears from the cache folder without the cache index knowing about it.

Definition at line 36 of file CachedFolder.cs.


The documentation for this class was generated from the following file: