   }
   
   internal func perform(url: NSURL) -> (UIImage?, isCache:Bool) {
-    dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER)
-    let maybeCache = cacheRead(url)
-    dispatch_semaphore_signal(semaphore)
+    dispatch_semaphore_wait(memorySemaphore,DISPATCH_TIME_FOREVER)
+    let maybeCache = memoryCacheRead(url)
+    dispatch_semaphore_signal(memorySemaphore)
     if let cache = maybeCache {
       return (cache, true)
     }
     let maybeImage = download(url)
     if let (image, byteSize) = maybeImage {
       if byteSize < config.maxByteSize {
-        dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER)
-        cacheWrite(url, image: image)
-        dispatch_semaphore_signal(semaphore)
+        dispatch_semaphore_wait(memorySemaphore,DISPATCH_TIME_FOREVER)
+        memoryCacheWrite(url, image: image)
+        dispatch_semaphore_signal(memorySemaphore)
       }
     }
     return (maybeImage?.0, false)

  }
  
  public func setConfig(config :Config) {
    dispatch_semaphore_wait(semaphore,DISPATCH_TIME_FOREVER)
    self.config = config
    dispatch_semaphore_signal(semaphore)
  }
  
  private var cache : [cacheImage] = []
  private var semaphore = dispatch_semaphore_create(1)

  private func cacheRead(url: NSURL) -> UIImage? {
    for var i=0; i<cache.count; i++ {
      if url == cache[i].url {
        cache[i].timeStamp = now
        return cache[i].image
      }
    }
    return nil
  }
  
  private func cacheWrite(url:NSURL,image:UIImage) {
    switch cache.count {
    case 0 ... config.maxCount:
      cache.append(cacheImage(image: image, timeStamp: now, url: url))
    case config.maxCount + 1://+1 because 0 origin
      var old = (0,now)
      for i in 0 ..< cache.count {
        if old.1 < cache[i].timeStamp {
          old = (i,cache[i].timeStamp)
        }
      }
      cache.removeAtIndex(old.0)
      cache.append(cacheImage(image: image, timeStamp:now, url: url))
    default:
      for _ in 0 ... 1 {
        var old = (0,now)
        for i in 0 ..< cache.count {
          if old.1 < cache[i].timeStamp {
            old = (i,cache[i].timeStamp)
          }
        }
        cache.removeAtIndex(old.0)
      }
      cache.append(cacheImage(image: image, timeStamp:now, url: url))      
    }
  }
  

    }
  }
  
  private struct memoryCacheImage {
    let image     :UIImage
    var timeStamp :Double
    let url       :NSURL
