Skip to content
This repository has been archived by the owner on Jun 4, 2018. It is now read-only.

Commit

Permalink
Added Caching container.
Browse files Browse the repository at this point in the history
Added Caching.IsoStoreProvider project.
  • Loading branch information
peterdrougge committed May 15, 2012
1 parent e7f2a90 commit 629d9ea
Show file tree
Hide file tree
Showing 37 changed files with 739 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Resources;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Usoniandream.WindowsPhone.LocationServices.Caching.AgFx")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Usoniandream.WindowsPhone.LocationServices.Caching.AgFx")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("9f7d09e6-7296-4d8b-905e-4f5afbedfc0a")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: NeutralResourcesLanguageAttribute("en-US")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>10.0.20506</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{8FFCE440-9265-4690-9F2C-29510CBF68D3}</ProjectGuid>
<ProjectTypeGuids>{C089C8C0-30E0-4E22-80C0-CE093F111A43};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Usoniandream.WindowsPhone.LocationServices.Caching.AgFx</RootNamespace>
<AssemblyName>Usoniandream.WindowsPhone.LocationServices.Caching.AgFx</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<SilverlightVersion>$(TargetFrameworkVersion)</SilverlightVersion>
<TargetFrameworkProfile>WindowsPhone71</TargetFrameworkProfile>
<TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier>
<SilverlightApplication>false</SilverlightApplication>
<ValidateXaml>true</ValidateXaml>
<ThrowErrorsInValidation>true</ThrowErrorsInValidation>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>Bin\Debug</OutputPath>
<DefineConstants>DEBUG;TRACE;SILVERLIGHT;WINDOWS_PHONE</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>Bin\Release</OutputPath>
<DefineConstants>TRACE;SILVERLIGHT;WINDOWS_PHONE</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="AgFx">
<HintPath>..\packages\AgFx.1.1.0.1101\lib\sl3-wp\AgFx.dll</HintPath>
</Reference>
<Reference Include="AgFx.Controls">
<HintPath>..\packages\AgFx.1.1.0.1101\lib\sl3-wp\AgFx.Controls.dll</HintPath>
</Reference>
<Reference Include="System.Windows" />
<Reference Include="system" />
<Reference Include="System.Core" />
<Reference Include="System.Xml" />
<Reference Include="System.Net" />
<Reference Include="mscorlib.extensions" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight for Phone\$(TargetFrameworkVersion)\Microsoft.Silverlight.$(TargetFrameworkProfile).Overrides.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight for Phone\$(TargetFrameworkVersion)\Microsoft.Silverlight.CSharp.targets" />
<ProjectExtensions />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AgFx" version="1.1.0.1101" />
</packages>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

//
// Copyright (c) 2012 Peter Drougge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;

namespace Usoniandream.WindowsPhone.LocationServices.Caching.IsoStoreCache
{
[DataContract]
public class IsoStoreCacheItem<T>
{
[DataMember]
public string ID { get; set; }
[DataMember]
public T[] Data { get; set; }
[DataMember]
public DateTime Created { get; set ; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@

//
// Copyright (c) 2012 Peter Drougge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Runtime.Serialization;
using System.Linq;
using System.Reactive.Linq;
using System.Diagnostics;
using System.Collections.Generic;

namespace Usoniandream.WindowsPhone.LocationServices.Caching.IsoStoreCache
{
public class IsolatedStorageCacheProvider : ICacheProvider
{

public bool IsValid<T>(SearchCriterias.ISearchCriteriaFoundation foundation, int duration)
{
IsoStoreCacheItem<T> item = null;
GetItemFromLocalSource<T>(foundation, (o) => { item = o; });
if (item!=null)
{
if (item.Created.AddSeconds(duration)>= DateTime.Now)
{
return true;
}
}
return false;
}

public bool IsCached(SearchCriterias.ISearchCriteriaFoundation foundation)
{
var filename = GetFilename(foundation);
using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (iso.FileExists(filename))
{
Debug.WriteLine("cached entry exists");
return true;
}
}
Debug.WriteLine("cached entry does not exists");
return false;
}

public void Remove(SearchCriterias.ISearchCriteriaFoundation foundation)
{
var filename = GetFilename(foundation);
using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (iso.FileExists(filename))
{
iso.DeleteFile(filename);
Debug.WriteLine("cache entry removed");
}
}
}

public void Add<T>(SearchCriterias.ISearchCriteriaFoundation foundation, System.IObservable<T> result)
{
SaveToLocalSource<T>(foundation, result);
}

public System.IObservable<T> GetCachedData<T>(SearchCriterias.ISearchCriteriaFoundation foundation)
{
IsoStoreCacheItem<T> ret = null;
GetItemFromLocalSource<T>(foundation, (o) => { ret = o;});

if (ret == null)
{
Debug.WriteLine("returning null since result wasn't found in cache");
return null;
}
Debug.WriteLine("returning observable result");
return ret.Data.ToObservable();
}

protected virtual string GetFilename(SearchCriterias.ISearchCriteriaFoundation foundation)
{
return String.Format("{0}.xml", foundation.Identifier);
}
protected virtual void GetItemFromLocalSource<T>(SearchCriterias.ISearchCriteriaFoundation foundation, Action<IsoStoreCacheItem<T>> callback)
{
var filename = GetFilename(foundation);
Object deserialized = null;
IsoStoreCacheItem<T> result = null;

using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
{
if (iso.FileExists(filename))
{
using (IsolatedStorageFileStream file = iso.OpenFile(filename, FileMode.Open))
{
try
{
DataContractSerializer serializer = new DataContractSerializer(typeof(IsoStoreCacheItem<T>));
deserialized = serializer.ReadObject(file);
}
catch (Exception ex)
{
// error, delete the file since we couldn't parse it correctly..
Debug.WriteLine(ex.Message);
}
}
}
}

result = deserialized as IsoStoreCacheItem<T>;
if (result == null && deserialized != null)
throw new InvalidCastException(String.Format("{0} is not in the right format.", filename));
if (result == null)
{
Debug.WriteLine("deserialized cache result is null");
callback(null);
}
if (result != null)
{
Debug.WriteLine("deserialized cache result, returning object");
callback(result);
}
}
protected void SaveToLocalSource<T>(SearchCriterias.ISearchCriteriaFoundation foundation, IObservable<T> items)
{
if (Busy)
{
return;
}

// if we never loaded in the data, don't bother saving it
if (items == null)
return;

Busy = true;

// note we might want to do all this work on another thread in a real system
var filename = GetFilename(foundation);

List<T> col = new List<T>(items.ToEnumerable<T>());

using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
if (iso.FileExists(filename))
iso.DeleteFile(filename);

IsoStoreCacheItem<T> item = new IsoStoreCacheItem<T>() { Data = col.ToArray(), Created = DateTime.Now };
using (IsolatedStorageFileStream file = iso.CreateFile(filename))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(IsoStoreCacheItem<T>));
serializer.WriteObject(file, item);
}
Debug.WriteLine("entry written to cache");
}
catch (Exception ex)
{
// error, delete the file since we couldn't parse it correctly..
Debug.WriteLine(ex.Message);
iso.DeleteFile(filename);
}
finally
{
Busy = false;
}
}
}

public bool Busy { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Resources;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Usoniandream.WindowsPhone.LocationServices.Caching.IsoStoreCache")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Usoniandream.WindowsPhone.LocationServices.Caching.IsoStoreCache")]
[assembly: AssemblyCopyright("Copyright © Peter Drougge 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("2b3bac4d-48c0-451e-a586-bc4c032f066d")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: NeutralResourcesLanguageAttribute("en-US")]
Loading

0 comments on commit 629d9ea

Please sign in to comment.