Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ Anything documented here is part of the public API that Flask-Session provides,
:members: regenerate

.. autoclass:: flask_session.redis.RedisSessionInterface
.. autoclass:: flask_session.valkey.ValkeySessionInterface
.. autoclass:: flask_session.memcached.MemcachedSessionInterface
.. autoclass:: flask_session.filesystem.FileSystemSessionInterface
.. autoclass:: flask_session.cachelib.CacheLibSessionInterface
.. autoclass:: flask_session.mongodb.MongoDBSessionInterface
.. autoclass:: flask_session.sqlalchemy.SqlAlchemySessionInterface
.. autoclass:: flask_session.dynamodb.DynamoDBSessionInterface
.. autoclass:: flask_session.postgresql.PostgreSqlSessionInterface
.. autoclass:: flask_session.postgresql.PostgreSqlSessionInterface
2 changes: 1 addition & 1 deletion docs/config_cleanup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ Run the the following command regularly with a cron job or scheduler such as Her

Alternatively, set the configuration variable ``SESSION_CLEANUP_N_REQUESTS`` to the average number of requests after which the cleanup should be performed. This is less desirable than using the scheduled app command cleanup as it may slow down some requests but may be useful for small applications or rapid development.

This is not required for the ``Redis``, ``Memecached``, ``Filesystem``, ``Mongodb`` storage engines, as they support time-to-live for records.
This is not required for the ``Redis``, ``Valkey``, ``Memecached``, ``Filesystem``, ``Mongodb`` storage engines, as they support time-to-live for records.
89 changes: 50 additions & 39 deletions docs/config_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Configuration Reference
=========================

.. include:: config_flask.rst

Flask-Session configuration values
----------------------------------

Expand All @@ -13,6 +13,7 @@ These are specific to Flask-Session.
Specifies which type of session interface to use. Built-in session types:

- **redis**: RedisSessionInterface
- **valkey**: ValkeySessionInterface
- **memcached**: MemcachedSessionInterface
- **filesystem**: FileSystemSessionInterface (Deprecated in 0.7.0, will be removed in 1.0.0 in favor of CacheLibSessionInterface)
- **cachelib**: CacheLibSessionInterface
Expand All @@ -23,7 +24,7 @@ These are specific to Flask-Session.
.. py:data:: SESSION_PERMANENT

Whether use permanent session or not.

Default: ``True``

.. py:data:: SESSION_USE_SIGNER
Expand All @@ -32,29 +33,29 @@ These are specific to Flask-Session.

.. note::
This feature is historical and generally only relevant if you are using client-side sessions ie. not Flask-Session. SESSION_ID_LENGTH provides the relevant entropy for session identifiers.

Default: ``False``

.. deprecated:: 0.7.0

.. py:data:: SESSION_KEY_PREFIX

A prefix that is added before all session keys. This makes it easier to use the same backend storage server for different apps.

Default: ``'session:'``

.. py:data:: SESSION_ID_LENGTH

The length of the session identifier in bytes (of entropy).

Default: ``32``

.. versionadded:: 0.6.0

.. py:data:: SESSION_SERIALIZATION_FORMAT

The serialization format to use. Can be `'msgpack'`` or `'json'`. Set to `'msgpack'`` for a more efficient serialization format. Set to `'json'`` for a human-readable format.

Default: ``'msgpack'``

.. versionadded:: 0.7.0
Expand All @@ -79,7 +80,17 @@ Redis
.. py:data:: SESSION_REDIS

A ``redis.Redis`` instance.


Default: Instance connected to ``127.0.0.1:6379``


Valkey
~~~~~~~~~~~~~~~~~~~~~~~

.. py:data:: SESSION_VALKEY

A ``valkey.Valkey`` instance.

Default: Instance connected to ``127.0.0.1:6379``


Expand All @@ -89,7 +100,7 @@ Memcached
.. py:data:: SESSION_MEMCACHED

A ``memcache.Client`` instance.

Default: Instance connected to ``127.0.0.1:6379``


Expand All @@ -99,23 +110,23 @@ FileSystem
.. py:data:: SESSION_FILE_DIR

The directory where session files are stored.

Default: ``flask_session`` directory under current working directory.

.. deprecated:: 0.7.0

.. py:data:: SESSION_FILE_THRESHOLD

The maximum number of items the session stores before it starts deleting some.

Default: ``500``

.. deprecated:: 0.7.0

.. py:data:: SESSION_FILE_MODE

The file mode wanted for the session files.

Default: ``0600``

.. deprecated:: 0.7.0
Expand All @@ -125,17 +136,17 @@ CacheLib
.. py:data:: SESSION_CACHELIB

Any valid `cachelib backend <https://cachelib.readthedocs.io/en/stable/>`_. This allows you maximum flexibility in choosing the cache backend and it's configuration.

The following would set a cache directory called "flask_session" and a threshold of 500 items before it starts deleting some.

.. code-block:: python

app.config['SESSION_CACHELIB'] = FileSystemCache(cache_dir='flask_session', threshold=500)

.. important::

A ``default_timeout`` set in any of the ``CacheLib`` backends will be overrode by the ``PERMANENT_SESSION_LIFETIME`` when each stored session's expiry is set.

Default: ``FileSystemCache`` in ``./flask_session`` directory.

MongoDB
Expand All @@ -144,19 +155,19 @@ MongoDB
.. py:data:: SESSION_MONGODB

A ``pymongo.MongoClient`` instance.

Default: Instance connected to ``127.0.0.1:27017``

.. py:data:: SESSION_MONGODB_DB

The MongoDB database you want to use.

Default: ``'flask_session'``

.. py:data:: SESSION_MONGODB_COLLECT

The MongoDB collection you want to use.

Default: ``'sessions'``


Expand All @@ -166,60 +177,60 @@ SqlAlchemy
.. py:data:: SESSION_SQLALCHEMY

A ``flask_sqlalchemy.SQLAlchemy`` instance whose database connection URI is configured using the ``SQLALCHEMY_DATABASE_URI`` parameter.

Must be set in flask_sqlalchemy version 3.0 or higher.

.. py:data:: SESSION_SQLALCHEMY_TABLE

The name of the SQL table you want to use.

Default: ``'sessions'``

.. py:data:: SESSION_SQLALCHEMY_SEQUENCE

The name of the sequence you want to use for the primary key.

Default: ``None``

.. py:data:: SESSION_SQLALCHEMY_SCHEMA

The name of the schema you want to use.

Default: ``None``

.. py:data:: SESSION_SQLALCHEMY_BIND_KEY

The name of the bind key you want to use.

Default: ``None``

.. py:data:: SESSION_CLEANUP_N_REQUESTS

Only applicable to non-TTL backends.

The average number of requests after which Flask-Session will perform a session cleanup. This involves removing all session data that is older than ``PERMANENT_SESSION_LIFETIME``. Using the app command ``flask session_cleanup`` instead is preferable.

Default: ``None``

Dynamodb
~~~~~~~~~~~~~~~~~~~~~~~

.. py:data:: SESSION_DYNAMODB

A ``boto3.resource`` instance.

Default: Instance connected to ``'localhost:8000'``

.. py:data:: SESSION_DYNAMODB_TABLE_NAME

The name of the table you want to use.

Default: ``'Sessions'``

.. py:data:: SESSION_DYNAMODB_TABLE_EXISTS

By default it will create a new table with the TTL setting activated unless you set this parameter to ``True``, then it assumes that the table already exists.

Default: ``False``

.. deprecated:: 0.7.0
Expand Down
13 changes: 10 additions & 3 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Install from PyPI using an installer such as pip:

Flask-Session's only required dependency is msgspec for serialization, which has no sub-dependencies.

However, you also need to choose a storage type and install an appropriate client library so the app can communicate with storage.
However, you also need to choose a storage type and install an appropriate client library so the app can communicate with storage.
For example, if you want to use Redis as your storage, you will need to install the redis-py_ library either directly or as an optional dependency like below:

.. code-block:: bash
Expand Down Expand Up @@ -48,10 +48,14 @@ Available storage options and their corresponding ``<storage-option>`` values ar
- ``redis``
- redis-py_
-
* - **Valkey**
- ``valkey``
- valkey-py_
-
* - **Memcached**
- ``memcached``
- pymemcache_
- pylibmc_, python-memcached_, libmc_
- pylibmc_, python-memcached_, libmc_
* - **MongoDB**
- ``mongodb``
- pymongo_
Expand All @@ -74,7 +78,7 @@ Other storage backends might be compatible with Flask-Session as long as they ad
Cachelib
--------

Flask-Session also indirectly supports storage and client libraries via cachelib_, which is a wrapper around various cache libraries.
Flask-Session also indirectly supports storage and client libraries via cachelib_, which is a wrapper around various cache libraries.
You must also install cachelib_ itselfand the relevant client library to use these.

.. list-table::
Expand All @@ -91,6 +95,8 @@ You must also install cachelib_ itselfand the relevant client library to use the
- uwsgi_
* - Redis
- redis-py_
* - Valkey
- valkey-py_
* - Memcached
- pylibmc_, python-memcached_, libmc_ or `google.appengine.api.memcached`_
* - MongoDB
Expand All @@ -105,6 +111,7 @@ You must also install cachelib_ itselfand the relevant client library to use the


.. _redis-py: https://github.com/redis/redis-py
.. _valkey-py: https://github.com/valkey-io/valkey-py
.. _pylibmc: http://sendapatch.se/projects/pylibmc/
.. _python-memcached: https://github.com/linsomniac/python-memcached
.. _pymemcache: https://github.com/pinterest/pymemcache
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ dev-dependencies = [
"pytest>=7.4.4",
"pytest-cov>=4.1.0",
"redis>=5.0.3",
"valkey>=6.0.0",
"python-memcached>=1.62",
"flask-sqlalchemy>=3.0.5",
"pymongo>=4.6.2",
Expand Down
12 changes: 11 additions & 1 deletion src/flask_session/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .defaults import Defaults

__version__ = "0.8.0"
__version__ = "0.9.0"


class Session:
Expand Down Expand Up @@ -61,6 +61,9 @@ def _get_interface(self, app):
# Redis settings
SESSION_REDIS = config.get("SESSION_REDIS", Defaults.SESSION_REDIS)

# Valkey settings
SESSION_VALKEY = config.get("SESSION_VALKEY", Defaults.SESSION_VALKEY)

# Memcached settings
SESSION_MEMCACHED = config.get("SESSION_MEMCACHED", Defaults.SESSION_MEMCACHED)

Expand Down Expand Up @@ -144,6 +147,13 @@ def _get_interface(self, app):
**common_params,
client=SESSION_REDIS,
)
elif SESSION_TYPE == "valkey":
from .valkey import ValkeySessionInterface

session_interface = ValkeySessionInterface(
**common_params,
client=SESSION_VALKEY,
)
elif SESSION_TYPE == "memcached":
from .memcached import MemcachedSessionInterface

Expand Down
5 changes: 4 additions & 1 deletion src/flask_session/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Defaults:
# Redis settings
SESSION_REDIS = None

# Valkey settings
SESSION_VALKEY = None

# Memcached settings
SESSION_MEMCACHED = None

Expand Down Expand Up @@ -48,4 +51,4 @@ class Defaults:
# PostgreSQL settings
SESSION_POSTGRESQL = None
SESSION_POSTGRESQL_TABLE = "flask_sessions"
SESSION_POSTGRESQL_SCHEMA = "public"
SESSION_POSTGRESQL_SCHEMA = "public"
1 change: 1 addition & 0 deletions src/flask_session/valkey/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .valkey import ValkeySession, ValkeySessionInterface # noqa: F401
Loading
Loading