Skip to content

Commit

Permalink
Merge pull request #23 from /issues/21
Browse files Browse the repository at this point in the history
Issues/21 Change FileCache default savepath
  • Loading branch information
mattak authored May 6, 2017
2 parents b09d88d + ef47a43 commit 290a607
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
3 changes: 2 additions & 1 deletion Assets/Plugins/Unicache/Examples/Simple/SimpleComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ public class SimpleComponents : MonoBehaviour
public Button ClearImageButton;
public RawImage Image;
public Text LoadingText;
private IUnicache cache = new FileCache();
private IUnicache cache;

void Start()
{
this.cache = new FileCache();
this.cache.Handler = new SimpleDownloadHandler();
this.cache.UrlLocator = new SimpleUrlLocator();
this.cache.CacheLocator = new SimpleCacheLocator();
Expand Down
12 changes: 6 additions & 6 deletions Assets/Plugins/Unicache/Examples/Versioning/VersionComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public class VersionComponents : MonoBehaviour, IUnicacheGetter
public Button ClearButton;
public RawImage Image;

private IUnicache cache = new FileCache();
private IUnicache _cache;
private int count = 0;

public IUnicache Cache
{
get { return this.cache; }
get { return this._cache = this._cache ?? new FileCache(); }
}

private Dictionary<string, string> versionMap = new Dictionary<string, string>()
Expand All @@ -29,9 +29,9 @@ public IUnicache Cache

void Start()
{
this.cache.CacheLocator = new VersionCacheLocator(this.versionMap);
this.cache.Handler = new SimpleDownloadHandler();
this.cache.UrlLocator = new VersionUrlLocator();
this.Cache.CacheLocator = new VersionCacheLocator(this.versionMap);
this.Cache.Handler = new SimpleDownloadHandler();
this.Cache.UrlLocator = new VersionUrlLocator();

this.DownloadButton.onClick.AddListener(this.Download);
this.VersionUpButton.onClick.AddListener(this.VersionUp);
Expand All @@ -42,7 +42,7 @@ void Start()

void Download()
{
this.cache.Fetch("cachepig")
this.Cache.Fetch("cachepig")
.ByteToTexture2D(name: "cachepig")
.Subscribe(texture =>
{
Expand Down
33 changes: 19 additions & 14 deletions Assets/Plugins/Unicache/Scripts/FileCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ public class FileCache : IUnicache
public ICacheHandler Handler { get; set; }
public IUrlLocator UrlLocator { get; set; }
public ICacheLocator CacheLocator { get; set; }
private string RootDirectory;

public FileCache()
public FileCache() : this(UnicacheConfig.Directory)
{
}

public FileCache(string rootDirectory)
{
this.RootDirectory = rootDirectory;
}

public IObservable<byte[]> Fetch(string key)
{
var url = this.UrlLocator.CreateUrl(key);
Expand Down Expand Up @@ -46,7 +52,7 @@ public void Clear()
{
try
{
IO.CleanDirectory(UnicacheConfig.Directory);
IO.CleanDirectory(this.RootDirectory);
}
catch (DirectoryNotFoundException)
{
Expand All @@ -57,8 +63,7 @@ public void Delete(string key)
{
try
{
var allPathes = Directory.GetFiles(UnicacheConfig.Directory)
.Select(fullpath => fullpath.Replace(UnicacheConfig.Directory, ""));
var allPathes = ListPathes();
var keyPathes = new List<string>(this.CacheLocator.GetSameKeyCachePathes(key, allPathes));

foreach (var path in keyPathes)
Expand All @@ -73,45 +78,45 @@ public void Delete(string key)

public void DeleteByPath(string path)
{
File.Delete(UnicacheConfig.Directory + path);
File.Delete(this.RootDirectory + path);
}

public byte[] GetCache(string key)
{
return this.GetCacheByPath(this.CacheLocator.CreateCachePath(key));
}

private byte[] GetCacheByPath(string path)
protected byte[] GetCacheByPath(string path)
{
return IO.Read(UnicacheConfig.Directory + path);
return IO.Read(this.RootDirectory + path);
}

public void SetCache(string key, byte[] data)
{
this.SetCacheByPath(this.CacheLocator.CreateCachePath(key), data);
}

private void SetCacheByPath(string path, byte[] data)
protected void SetCacheByPath(string path, byte[] data)
{
IO.MakeParentDirectory(UnicacheConfig.Directory + path);
IO.Write(UnicacheConfig.Directory + path, data);
IO.MakeParentDirectory(this.RootDirectory + path);
IO.Write(this.RootDirectory + path, data);
}

public bool HasCache(string key)
{
return this.HasCacheByPath(this.CacheLocator.CreateCachePath(key));
}

private bool HasCacheByPath(string path)
protected bool HasCacheByPath(string path)
{
return File.Exists(UnicacheConfig.Directory + path);
return File.Exists(this.RootDirectory + path);
}

public IEnumerable<string> ListPathes()
{
return new List<string>(
IO.RecursiveListFiles(UnicacheConfig.Directory)
.Select(fullpath => fullpath.Replace(UnicacheConfig.Directory, ""))
IO.RecursiveListFiles(this.RootDirectory)
.Select(fullpath => fullpath.Replace(this.RootDirectory, ""))
);
}
}
Expand Down

0 comments on commit 290a607

Please sign in to comment.