Skip to content

Commit

Permalink
issues/20 chore(IUnicache): handle cache encoder, decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
mattak committed May 7, 2017
1 parent 6f7772f commit ccc16c0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
15 changes: 11 additions & 4 deletions Assets/Plugins/Unicache/Scripts/FileCache.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Unicache.Plugin;
using UniRx;

namespace Unicache
Expand All @@ -10,15 +11,19 @@ public class FileCache : IUnicache
public ICacheHandler Handler { get; set; }
public IUrlLocator UrlLocator { get; set; }
public ICacheLocator CacheLocator { get; set; }
public ICacheEncoder Encoder { get; set; }
public ICacheDecoder Decoder { get; set; }
private string RootDirectory;

public FileCache() : this(UnicacheConfig.Directory)
public FileCache() : this(UnicacheConfig.Directory, new VoidEncoderDecoder())
{
}

public FileCache(string rootDirectory)
public FileCache(string rootDirectory, ICacheEncoderDecoder encoderDecoder)
{
this.RootDirectory = rootDirectory;
this.Encoder = encoderDecoder;
this.Decoder = encoderDecoder;
}

public IObservable<byte[]> Fetch(string key)
Expand Down Expand Up @@ -88,7 +93,8 @@ public byte[] GetCache(string key)

protected byte[] GetCacheByPath(string path)
{
return IO.Read(this.RootDirectory + path);
var data = IO.Read(this.RootDirectory + path);
return this.Decoder.Decode(data);
}

public void SetCache(string key, byte[] data)
Expand All @@ -98,8 +104,9 @@ public void SetCache(string key, byte[] data)

protected void SetCacheByPath(string path, byte[] data)
{
byte[] writeData = this.Encoder.Encode(data);
IO.MakeParentDirectory(this.RootDirectory + path);
IO.Write(this.RootDirectory + path, data);
IO.Write(this.RootDirectory + path, writeData);
}

public bool HasCache(string key)
Expand Down
2 changes: 2 additions & 0 deletions Assets/Plugins/Unicache/Scripts/IUnicache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface IUnicache
ICacheHandler Handler { set; }
IUrlLocator UrlLocator { set; }
ICacheLocator CacheLocator { set; }
ICacheEncoder Encoder { set; }
ICacheDecoder Decoder { set; }

IObservable<byte[]> Fetch(string key);
void Clear();
Expand Down
17 changes: 14 additions & 3 deletions Assets/Plugins/Unicache/Scripts/MemoryCache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Unicache.Plugin;
using UniRx;

namespace Unicache
Expand All @@ -8,13 +9,21 @@ public class MemoryCache : IUnicache
public ICacheHandler Handler { get; set; }
public IUrlLocator UrlLocator { get; set; }
public ICacheLocator CacheLocator { get; set; }
public ICacheEncoder Encoder { get; set; }
public ICacheDecoder Decoder { get; set; }

private IDictionary<string, byte[]> MemoryMap = new Dictionary<string, byte[]>();

public MemoryCache()
public MemoryCache() : this(new VoidEncoderDecoder())
{
}

public MemoryCache(ICacheEncoderDecoder encoderDecoder)
{
this.Encoder = encoderDecoder;
this.Decoder = encoderDecoder;
}

public IObservable<byte[]> Fetch(string key)
{
var url = this.UrlLocator.CreateUrl(key);
Expand Down Expand Up @@ -70,7 +79,8 @@ public byte[] GetCache(string key)

private byte[] GetCacheByPath(string path)
{
return this.MemoryMap[path];
var data = this.MemoryMap[path];
return this.Decoder.Decode(data);
}

public void SetCache(string key, byte[] data)
Expand All @@ -80,7 +90,8 @@ public void SetCache(string key, byte[] data)

private void SetCacheByPath(string path, byte[] data)
{
this.MemoryMap[path] = data;
var writeData = this.Encoder.Encode(data);
this.MemoryMap[path] = writeData;
}

public bool HasCache(string key)
Expand Down
3 changes: 2 additions & 1 deletion Assets/Plugins/Unicache/Scripts/UnicacheConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
using Unicache.Plugin;
using UnityEngine;

namespace Unicache
{
Expand Down

0 comments on commit ccc16c0

Please sign in to comment.