def typedget(cast, db, key, field=None, default=None):
    """Returns the value of the given key (and field if a hash) as the given type.
    If not found, returns the given default (None as default)."""
    try:
        if field:
            return cast(db.hget(key, field))
        else:
            return cast(db.get(key))
    except TypeError:
        return default