Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
URLCache Struct Reference

#include <URLCache.h>

Overview

A cache for HTTP responses.

Definition at line 44 of file URLCache.h.

Inheritance diagram for URLCache:
Object

Properties

size_t currentSize
 The current cached body size.
 
Locklock
 The lock protecting this cache.
 
size_t maxSize
 The maximum cached body size.
 
Object object
 The superclass.
 
Dictionaryresponses
 The cached responses, keyed by URLRequest.
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class.
 
unsigned int magic
 A header to allow introspection of Object types.
 

Methods

Class_URLCache (void)
 The URLCache archetype.
 
URLCachedResponsecachedResponseForRequest (URLCache *self, const URLRequest *request)
 Returns the cached response for the given request.
 
URLCacheinit (URLCache *self)
 Initializes this URLCache.
 
void removeAllCachedResponses (URLCache *self)
 Removes all cached responses.
 
void removeCachedResponseForRequest (URLCache *self, const URLRequest *request)
 Removes the cached response for the given request.
 
void setMaxSize (URLCache *self, size_t maxSize)
 Sets the maximum cached body size.
 
void storeCachedResponseForRequest (URLCache *self, const URLRequest *request, const URLResponse *response, const Data *data)
 Stores a cached response for the given request.
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype.
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object.
 
bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object.
 
bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership.
 

Protected Attributes

URLCacheInterface * interface
 The interface.
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface.
 

Property Details

◆ currentSize

size_t URLCache::currentSize

The current cached body size.

Definition at line 70 of file URLCache.h.

◆ interface

URLCacheInterface* URLCache::interface
protected

The interface.

Definition at line 55 of file URLCache.h.

◆ lock

Lock* URLCache::lock

The lock protecting this cache.

Definition at line 60 of file URLCache.h.

◆ maxSize

size_t URLCache::maxSize

The maximum cached body size.

Definition at line 75 of file URLCache.h.

◆ object

Object URLCache::object

The superclass.

Definition at line 49 of file URLCache.h.

◆ responses

Dictionary* URLCache::responses

The cached responses, keyed by URLRequest.

Definition at line 65 of file URLCache.h.

Method Details

◆ _URLCache()

Class * _URLCache ( void  )

The URLCache archetype.

Returns
The URLCache Class.

Definition at line 411 of file URLCache.c.

411 {
412 static Class *clazz;
413 static Once once;
414
415 do_once(&once, {
416 clazz = _initialize(&(const ClassDef) {
417 .name = "URLCache",
418 .superclass = _Object(),
419 .instanceSize = sizeof(URLCache),
420 .interfaceOffset = offsetof(URLCache, interface),
421 .interfaceSize = sizeof(URLCacheInterface),
423 });
424 });
425
426 return clazz;
427}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
long Once
The Once type.
Definition Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition Class.h:41
The runtime representation of a Class.
Definition Class.h:95
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition Object.h:55
Class * _Object(void)
The Object archetype.
Definition Object.c:136
A cache for HTTP responses.
Definition URLCache.h:44
URLCacheInterface * interface
The interface.
Definition URLCache.h:55

◆ cachedResponseForRequest()

URLCachedResponse * cachedResponseForRequest ( URLCache self,
const URLRequest request 
)

Returns the cached response for the given request.

Parameters
selfThe URLCache.
requestThe URLRequest.
Returns
The cached response, retained, or NULL if none is cached.

Definition at line 268 of file URLCache.c.

268 {
269
270 if (requestIsCacheable(request) == false) {
271 return NULL;
272 }
273
274 URLCachedResponse *cachedResponse = NULL;
275 const Dictionary *dictionary = (Dictionary *) self->responses;
276 synchronized(self->lock, {
277 cachedResponse = $(dictionary, objectForKey, (ident) request);
278 if (cachedResponse) {
279 retain(cachedResponse);
280 }
281 });
282
283 return cachedResponse;
284}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:210
static Dictionary * dictionary(void)
Definition Dictionary.c:242
static ident objectForKey(const Dictionary *self, const ident key)
Definition Dictionary.c:439
static int request(RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, Data **out_data)
Definition RESTClient.c:57
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
static bool requestIsCacheable(const URLRequest *request)
Returns true if this request is cacheable.
Definition URLCache.c:161
Key-value stores.
Definition Dictionary.h:60
Lock * lock
The lock protecting this cache.
Definition URLCache.h:60
Dictionary * responses
The cached responses, keyed by URLRequest.
Definition URLCache.h:65
A cached HTTP response.

◆ init()

URLCache * init ( URLCache self)

Initializes this URLCache.

Parameters
selfThe URLCache.
Returns
The initialized URLCache, or NULL on error.

Definition at line 290 of file URLCache.c.

290 {
291
292 self = (URLCache *) super(Object, self, init);
293 if (self) {
294 self->lock = $(alloc(Lock), init);
295 assert(self->lock);
296
297 self->responses = $(alloc(Dictionary), init);
298 assert(self->responses);
299
301 }
302
303 return self;
304}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
#define super(type, obj, method,...)
#define URLCACHE_DEFAULT_MAX_SIZE
Definition URLCache.c:33
POSIX Threads locks.
Definition Lock.h:42
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
size_t maxSize
The maximum cached body size.
Definition URLCache.h:75
URLCache * init(URLCache *self)
Initializes this URLCache.
Definition URLCache.c:290

◆ removeAllCachedResponses()

void removeAllCachedResponses ( URLCache self)

Removes all cached responses.

Parameters
selfThe URLCache.

Definition at line 310 of file URLCache.c.

310 {
311
312 synchronized(self->lock, {
313 $(self->responses, removeAllObjects);
314 self->currentSize = 0;
315 });
316}
static void removeAllObjects(Array *self)
Definition Array.c:604
size_t currentSize
The current cached body size.
Definition URLCache.h:70

◆ removeCachedResponseForRequest()

void removeCachedResponseForRequest ( URLCache self,
const URLRequest request 
)

Removes the cached response for the given request.

Parameters
selfThe URLCache.
requestThe URLRequest.

Definition at line 322 of file URLCache.c.

322 {
323
324 synchronized(self->lock, {
325 const Dictionary *dictionary = (Dictionary *) self->responses;
326 URLCachedResponse *cachedResponse = $(dictionary, objectForKey, (ident) request);
327 if (cachedResponse) {
328 self->currentSize -= cachedResponse->size;
330 }
331 });
332}
static void removeObjectForKey(Dictionary *self, const ident key)
Definition Dictionary.c:547
size_t size
The cached body size.

◆ setMaxSize()

void setMaxSize ( URLCache self,
size_t  maxSize 
)

Sets the maximum cached body size.

Parameters
selfThe URLCache.
maxSizeThe maximum cache size.

Definition at line 338 of file URLCache.c.

338 {
339
340 synchronized(self->lock, {
341 self->maxSize = maxSize;
342
343 if (self->maxSize == 0) {
344 $(self->responses, removeAllObjects);
345 self->currentSize = 0;
346 } else {
347 evictIfNeeded(self);
348 }
349 });
350}
static void evictIfNeeded(URLCache *self)
Removes the oldest cached response while this cache exceeds its maximum size.
Definition URLCache.c:213

◆ storeCachedResponseForRequest()

void storeCachedResponseForRequest ( URLCache self,
const URLRequest request,
const URLResponse response,
const Data data 
)

Stores a cached response for the given request.

Parameters
selfThe URLCache.
requestThe URLRequest.
responseThe HTTP response.
dataThe response body.

Definition at line 356 of file URLCache.c.

357 {
358
359 if (requestIsCacheable(request) == false || responseIsCacheable(response) == false) {
360 return;
361 }
362
363 URLCachedResponse *cachedResponse = $(alloc(URLCachedResponse), initWithResponseData, response, data);
364 if (cachedResponse == NULL) {
365 return;
366 }
367
368 synchronized(self->lock, {
369 if (cachedResponse->size <= self->maxSize && self->maxSize > 0) {
370
371 const Dictionary *dictionary = (Dictionary *) self->responses;
373 if (existing) {
374 self->currentSize -= existing->size;
376 }
377
378 URLRequest *key = (URLRequest *) $((Object *) request, copy);
379 $(self->responses, setObjectForKey, cachedResponse, key);
380 release(key);
381
382 self->currentSize += cachedResponse->size;
383 evictIfNeeded(self);
384 }
385 });
386
387 release(cachedResponse);
388}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
static Data * data(void)
Definition Data.c:286
static void setObjectForKey(Dictionary *self, const ident obj, const ident key)
Definition Dictionary.c:634
static bool responseIsCacheable(const URLResponse *response)
Returns true if this response is cacheable.
Definition URLCache.c:191
static URLCachedResponse * initWithResponseData(URLCachedResponse *self, const URLResponse *response, const Data *data)
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition Array.c:84
A protocol-agnostic abstraction for requesting resources via URLs.
Definition URLRequest.h:58

The documentation for this struct was generated from the following files: