Permalink
Browse files

Added Caching container.

Added Caching.IsoStoreProvider project.
  • Loading branch information...
1 parent e7f2a90 commit 629d9eab99235af27d06cc87cb01466ec726ce8d @peterdrougge committed May 15, 2012
Showing with 739 additions and 24 deletions.
  1. +37 −0 Usoniandream.WindowsPhone.LocationServices.Caching.AgFx/Properties/AssemblyInfo.cs
  2. +76 −0 ...hone.LocationServices.Caching.AgFx/Usoniandream.WindowsPhone.LocationServices.Caching.AgFx.csproj
  3. +4 −0 Usoniandream.WindowsPhone.LocationServices.Caching.AgFx/packages.config
  4. +41 −0 Usoniandream.WindowsPhone.LocationServices.IsoStoreCache/IsoStoreCacheItem.cs
  5. +193 −0 Usoniandream.WindowsPhone.LocationServices.IsoStoreCache/IsolatedStorageCacheProvider.cs
  6. +37 −0 Usoniandream.WindowsPhone.LocationServices.IsoStoreCache/Properties/AssemblyInfo.cs
  7. +86 −0 ...ne.LocationServices.IsoStoreCache/Usoniandream.WindowsPhone.LocationServices.IsoStoreCache.csproj
  8. +5 −0 Usoniandream.WindowsPhone.LocationServices.IsoStoreCache/packages.config
  9. +72 −0 Usoniandream.WindowsPhone.LocationServices/Caching/ICacheProvider.cs
  10. +1 −1 Usoniandream.WindowsPhone.LocationServices/Models/IService.cs
  11. +40 −0 Usoniandream.WindowsPhone.LocationServices/SearchCriterias/CriteriaCacheSettings.cs
  12. +2 −18 Usoniandream.WindowsPhone.LocationServices/SearchCriterias/ISearchCriteria.cs
  13. +44 −0 Usoniandream.WindowsPhone.LocationServices/SearchCriterias/ISearchCriteriaFoundation.cs
  14. +9 −1 Usoniandream.WindowsPhone.LocationServices/SearchCriterias/SearchCriteriaBase.cs
  15. +12 −0 Usoniandream.WindowsPhone.LocationServices/Service/GenericServiceLayer.cs
  16. +63 −4 Usoniandream.WindowsPhone.LocationServices/Service/Reactive/GenericServiceLayer.cs
  17. +3 −0 Usoniandream.WindowsPhone.LocationServices/Usoniandream.WindowsPhone.LocationServices.csproj
  18. +4 −0 ...ndream.WindowsPhone.OpenStockholm.Tester/Usoniandream.WindowsPhone.LocationServices.Tester.csproj
  19. +8 −0 Usoniandream.WindowsPhone.sln
  20. BIN output/release/Usoniandream.WindowsPhone.LocationServices.Bing.dll
  21. BIN output/release/Usoniandream.WindowsPhone.LocationServices.Caching.IsoStoreCache.dll
  22. BIN output/release/Usoniandream.WindowsPhone.LocationServices.Commute.dll
  23. BIN output/release/Usoniandream.WindowsPhone.LocationServices.Flickr.dll
  24. BIN output/release/Usoniandream.WindowsPhone.LocationServices.Google.dll
  25. BIN output/release/Usoniandream.WindowsPhone.LocationServices.Goteborg.dll
  26. BIN output/release/Usoniandream.WindowsPhone.LocationServices.Instagram.dll
  27. BIN output/release/Usoniandream.WindowsPhone.LocationServices.Nokia.dll
  28. BIN output/release/Usoniandream.WindowsPhone.LocationServices.Orebro.dll
  29. BIN output/release/Usoniandream.WindowsPhone.LocationServices.Stockholm.AR.dll
  30. BIN output/release/Usoniandream.WindowsPhone.LocationServices.Stockholm.dll
  31. BIN output/release/Usoniandream.WindowsPhone.LocationServices.Twitter.dll
  32. BIN output/release/Usoniandream.WindowsPhone.LocationServices.dll
  33. BIN packages/AgFx.1.1.0.1101/AgFx.1.1.0.1101.nupkg
  34. BIN packages/AgFx.1.1.0.1101/lib/sl3-wp/AgFx.Controls.dll
  35. BIN packages/AgFx.1.1.0.1101/lib/sl3-wp/AgFx.dll
  36. BIN packages/AgFx.1.1.0.1101/lib/sl4/AgFx.dll
  37. +2 −0 packages/repositories.config
@@ -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")]
@@ -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>
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="AgFx" version="1.1.0.1101" />
+</packages>
@@ -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 ; }
+ }
+}
@@ -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; }
+ }
+}
@@ -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")]
Oops, something went wrong. Retry.

0 comments on commit 629d9ea

Please sign in to comment.